简体   繁体   中英

putting multiple style of plots into one

I have the below arrays. I want to use the random_round_i arrays to create 3 interquartile plots, upper_limits to show the upper limit for each boxplot, base as a bottom line for all, and flow as an individual plot again.

I am not putting everything in boxplots, because flow, upper_limits and base are coming from different sources, and in fact I need to compare their values with the random

random_round_1=[0.508477, 0.509855, 0.517986]
random_round_2=[0.506998, 0.523818, 0.503029]
random_round_3=[0.524584, 0.53033, 0.514867]
flow = [0.503688, 0.507809, 0.504012]
upper_limits = [0.544946, 0.568013, 0.616112]
base = [0.481581] 

How can I create one plot, in shich I have base as one horizontal long line below all boxplots, then the box plots next to each other, and finally flow and upper_limits as normal plots.

在此处输入图片说明

When I just use the code below, the boxplots and the line plots don't match at the correct positions. I want those lines to go through all three boxplots.

    plt.boxplot([random_round_1, random_round_2, random_round_3])
    plt.plot(upper_limits)
    plt.plot(flow)
    plt.hlines(y = base, xmin = 0, xmax = 4)

The problem is that your box plots are centered at x = 1, 2 and 3. However, when you use plt.plot(upper_limits) and plt.plot(flow) , you are just passing the y-values. In this case, the default x-values starts from 0. Therefore, your curves are not falling at the same x-values on top of box plots.

You need the correct x-mesh starting from 1 which can be generated using either range or np.arange .

plt.boxplot([random_round_1, random_round_2, random_round_3])
plt.plot(range(1, len(upper_limits)+1), upper_limits)
plt.plot(range(1, len(flow)+1), flow)
plt.hlines(y = base, xmin = 0, xmax = 4)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM