在matplotlib中显示折线图的最大值

时间:2019-05-27 06:58:25

标签: python matplotlib plot graph

这是我当前的图表。
enter image description here
而且,我想画一条线以显示图形的最大值,以及x轴上的值。看起来像这样:
enter image description here

我可以通过绘制线条来手动绘制线条,但是我被困在x轴上写入它的值。这是我想到的最好的:

enter image description here

但是可以看到,其他文字不在轴上。有没有办法显示所需的输出并使附加文本在轴上看起来更自然?有功能吗?

代码:

max_y = max(y)
max_x = []
for i in list(new_dict.keys()):
    if new_dict[i] == max_y:
        max_x.append(i)

plt.plot(x, y)
plt.plot([np.mean(max_x), np.mean(max_x)], [0, max_y], 'r--',label='Highest Probability')
plt.plot([min(x), np.mean(max_x)],[max_y,max_y],'r--')
plt.text(np.mean(max_x),0,str(np.mean(max_x)),fontsize='x-large')
plt.text(min(x),np.mean(max_y),str(np.mean(max_y)),fontsize='x-large')
plt.legend(loc='best')
plt.show()

You can find the data here

非常感谢!

0 个答案:

没有答案