Matplotlib boxplot using precalculated (summary) statistics

前端 未结 3 1018
抹茶落季
抹茶落季 2020-12-14 10:13

I need to do a boxplot (in Python and matplotlib) but I do not have the original \"raw\" data. What I have are precalculated values for max, min, mean, median and IQR (norma

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-14 10:40

    In the old versions, you have to manually do it by changing boxplot elements individually:

    Mean=[3.4] #mean
    IQR=[3.0,3.9] #inter quantile range
    CL=[2.0,5.0] #confidence limit
    A=np.random.random(50)
    D=plt.boxplot(A) # a simple case with just one variable to boxplot
    D['medians'][0].set_ydata(Mean)
    D['boxes'][0]._xy[[0,1,4], 1]=IQR[0]
    D['boxes'][0]._xy[[2,3],1]=IQR[1]
    D['whiskers'][0].set_ydata(np.array([IQR[0], CL[0]]))
    D['whiskers'][1].set_ydata(np.array([IQR[1], CL[1]]))
    D['caps'][0].set_ydata(np.array([CL[0], CL[0]]))
    D['caps'][1].set_ydata(np.array([CL[1], CL[1]]))
    _=plt.ylim(np.array(CL)+[-0.1*np.ptp(CL), 0.1*np.ptp(CL)]) #reset the limit
    

    enter image description here

提交回复
热议问题