如何在箱线图上绘制中间值?

时间:2019-09-09 02:02:58

标签: python-3.x matplotlib

我有这个箱线图,但很难读取绿线的值。如何绘制该值?还是有另一种方法?

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以做的一件事就是直接根据数据框计算出的中位数绘图

In [204]: df
Out[204]: 
          a         b         c         d
0  0.807540  0.719843  0.291329  0.928670
1  0.449082  0.000000  0.575919  0.299698
2  0.703734  0.626004  0.582303  0.243273
3  0.363013  0.539557  0.000000  0.743613
4  0.185610  0.526161  0.795284  0.929223
5  0.000000  0.323683  0.966577  0.259640
6  0.000000  0.386281  0.000000  0.000000
7  0.500604  0.131910  0.413131  0.936908
8  0.992779  0.672049  0.108021  0.558684
9  0.797385  0.199847  0.329550  0.605690

In [205]: df.boxplot()
Out[205]: <matplotlib.axes._subplots.AxesSubplot at 0x103404d6a0>

In [206]: for s, x, y in zip(df.median().map('{:0.2f}'.format), np.arange(df.shape[0]) + 1, df.median()):
     ...:     plt.text(x, y, s, va='center', ha='center')

enter image description here