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

问题描述 投票:-1回答:1

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)
python matplotlib plot histogram
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.