用matplotlib显示每行的最终y轴值

问题描述

|| 我正在使用matplotlib绘制带有一些线的图形,我想在每条线在右侧结束的位置旁边显示最终的
y
值,如下所示: 是否有解决方案或指向API相关部分的指针?我很沮丧。 我正在使用matplotlib 1.0.0和pyplot接口,例如
pyplot.plot(xs,ys,f,xs_,ys_,f_)
。     

解决方法

        选项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
    ,        尽管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()
这会将文本8点指向轴右侧的右侧,每个图的最大y值。您还可以添加箭头等。请参见http://matplotlib.sourceforge.net/users/annotations_guide.html(如果希望文本垂直于给定的y值居中,也可以更改垂直对齐方式。
va=\'center\'
。) 另外,它不依赖刻度位置,因此非常适合对数图等。根据轴边界的位置和其点的偏移量给出文本的位置具有很多优点,如果您开始缩放图等     ,        乔非常有用。仅一个细节。如果最终值不是最大值,则可以使用y [-1]。我添加了一条水平线来阐明。
gbm = np.log(np.cumsum(np.random.randn(10000))+10000)
plt.plot(gbm)
plt.annotate(\'%0.2f\' % gbm[-1],gbm[-1]),textcoords=\'offset points\')
plt.axhline(y=gbm[-1],color=\'y\',linestyle=\'-.\')
plt.show()
标有最终y轴值的图。