我正在使用matplotlib绘制一些线条的图表,并希望在每条线条的右侧显示最终的
有什么解决方案或指向相关API部分的指针吗?我很困惑。
我正在使用matplotlib 1.0.0和pyplot接口,例如
y
值,就像这样:
![enter image description here](https://istack.dev59.com/6VECZ.webp)
我正在使用matplotlib 1.0.0和pyplot接口,例如
pyplot.plot(xs, ys, f, xs_, ys_, f_)
。y
值,就像这样:
pyplot.plot(xs, ys, f, xs_, ys_, f_)
。虽然Ofri的回答没有错,但annotate
特别用于此目的:
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()
这将在每个图的最大y值处,使文本相对于轴右侧偏移8个点。如果您需要在给定y值上垂直居中文本,则还可以添加箭头等内容。请参阅http://matplotlib.sourceforge.net/users/annotations_guide.html (如果指定va='center'
,还可以更改垂直对齐方式)。此外,这不依赖于刻度位置,因此对于对数图等情况也能完美工作。通过以轴边界的位置和其点偏移量来确定文本位置,在重新缩放绘图等情况下有很多优势。
plt.annotate('%0.2f' % var[-1], xy=(1, var.max()), xytext=(8, 0),
xycoords=('axes fraction', 'data'), textcoords='offset points')
- Frumda GrayforceOption 1 - pyplot.text
pyplot.text(x, y, string, fontdict=None, withdash=False, **kwargs)
选项2 - 使用第二个坐标轴:
second_axes = pyplot.twinx() # create the second axes, sharing x-axis
second_axis.set_yticks([0.2,0.4]) # list of your y values
pyplot.show() # update the figure
gbm = np.log(np.cumsum(np.random.randn(10000))+10000)
plt.plot(gbm)
plt.annotate('%0.2f' % gbm[-1], xy=(1, gbm[-1]), xytext=(8, 0),
xycoords=('axes fraction', 'data'), textcoords='offset points')
plt.axhline(y=gbm[-1], color='y', linestyle='-.')
plt.show()