关于python:用matplotlib显示每一行的最终y轴值

Show the final y-axis value of each line with matplotlib

我正在使用 matplotlib 绘制带有一些线的图形,我想在每条线在右侧结束的位置旁边显示最终的 y 值,如下所示:
enter


虽然 Ofri 的回答没有任何问题,但 annotate 专门用于此目的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(61).astype(np.float)
y1 = np.exp(0.1 * x)
y2 = np.exp(0.09 * x)

plt.plot(x, y1)
plt.plot(x, y2)

for var in (y1, y2):
    plt.annotate(‘%0.2f’ % var.max(), xy=(1, var.max()), xytext=(8, 0),
                 xycoords=(‘axes fraction’, ‘data’), textcoords=‘offset points’)

plt.show()

enter

1
pyplot.text(x, y, string, fontdict=None, withdash=False, **kwargs)

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/267876.html

(0)
上一篇 2022年6月19日
下一篇 2022年6月19日

相关推荐

发表回复

登录后才能评论