直方图显示图例中垂直线的值

时间:2018-07-30 09:14:39

标签: python matplotlib plot histogram

enter image description here

我可以在图例中显示垂直线(虚线)的值还是在某处添加注释,如何?

这是虚线的代码

plt.hist(df['wt_avg_delay'], bins=50, color='lightblue', edgecolor='black')
plt.axvline(df['wt_avg_delay'].mean(), color='orange', linestyle='dashed', linewidth=1)
plt.axvline(-19, color='green', linestyle='dashed', linewidth=1)
plt.axvline(27 color='red', linestyle='dashed', linewidth=1)

1 个答案:

答案 0 :(得分:1)

最简单的注释方法可能是使用plt.text()

plt.text(x, y, 'annotation')

或者,您也可以在行中添加标签:

import matplotlib.pyplot as plt

x = [1, 1, 1, 2, 2, 3]
p = 2.5


plt.hist(x, label='data')
plt.axvline(p, color='g', label=str(p))
plt.legend()
plt.show()

enter image description here

相关问题