I'm drawing a graph with some lines using matplotlib and I want to display the final y
value next to where each line ends on the right hand side like this:
我正在使用matplotlib绘制一些带有一些线条的图形,我想在右边的每一行的末尾显示最后的y值,如下所示:
Any solutions or pointers to the relevant parts of the API? I'm quite stumped.
API的相关部分的任何解决方案或指针?我很难过。
I'm using matplotlib 1.0.0 and the pyplot interface, e.g. pyplot.plot(xs, ys, f, xs_, ys_, f_)
.
我正在使用matplotlib 1.0.0和pyplot接口,例如pyplot.plot(xs,ys,f,xs_,ys_,f_)。
3 个解决方案
#1
4
Option 1 - pyplot.text
选项1 - pyplot.text
pyplot.text(x, y, string, fontdict=None, withdash=False, **kwargs)
Option 2 - Use a second axes:
选项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
#2
11
While there's nothing wrong with Ofri's answer, annotate
is intended especially for this purpose:
虽然Ofri的答案没有任何问题,但注释特别是为了这个目的:
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()
This places the text 8 points to the right of the right side of the axis, at the maximum y-value for each plot. You can also add in arrows, etc. See http://matplotlib.sourceforge.net/users/annotations_guide.html (You can also change the vertical alignment, if you want the text vertically centered on the given y-value. Just specify va='center'
.)
这将文本8个点放在轴右侧的右侧,每个绘图的最大y值。您还可以添加箭头等。请参阅http://matplotlib.sourceforge.net/users/annotations_guide.html(如果您希望文本垂直居中于给定的y值,也可以更改垂直对齐。只需指定VA = '中心'。)
Also, this doesn't rely on tick locations, so it will work perfectly for log plots, etc. Giving the location of the text in terms of the positions of the axis boundaries and its offset in points has a lot of advantages if you start rescaling the plot, etc.
此外,这不依赖于刻度位置,因此它可以完美地用于对数图等。如果您开始,根据轴边界的位置及其在点的偏移量给出文本的位置具有很多优点重新调整情节等
#3
1
Very useful Joe. Only one more detail. If the final value isn't a maximun, you can use y[-1]. I added a horizontal line to clarify.
非常有用乔。只有一个细节。如果最终值不是最大值,则可以使用y [-1]。我添加了一条水平线来澄清。
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()
Plot with final y-axis value marked.
标记最终y轴值的绘图。
#1
4
Option 1 - pyplot.text
选项1 - pyplot.text
pyplot.text(x, y, string, fontdict=None, withdash=False, **kwargs)
Option 2 - Use a second axes:
选项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
#2
11
While there's nothing wrong with Ofri's answer, annotate
is intended especially for this purpose:
虽然Ofri的答案没有任何问题,但注释特别是为了这个目的:
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()
This places the text 8 points to the right of the right side of the axis, at the maximum y-value for each plot. You can also add in arrows, etc. See http://matplotlib.sourceforge.net/users/annotations_guide.html (You can also change the vertical alignment, if you want the text vertically centered on the given y-value. Just specify va='center'
.)
这将文本8个点放在轴右侧的右侧,每个绘图的最大y值。您还可以添加箭头等。请参阅http://matplotlib.sourceforge.net/users/annotations_guide.html(如果您希望文本垂直居中于给定的y值,也可以更改垂直对齐。只需指定VA = '中心'。)
Also, this doesn't rely on tick locations, so it will work perfectly for log plots, etc. Giving the location of the text in terms of the positions of the axis boundaries and its offset in points has a lot of advantages if you start rescaling the plot, etc.
此外,这不依赖于刻度位置,因此它可以完美地用于对数图等。如果您开始,根据轴边界的位置及其在点的偏移量给出文本的位置具有很多优点重新调整情节等
#3
1
Very useful Joe. Only one more detail. If the final value isn't a maximun, you can use y[-1]. I added a horizontal line to clarify.
非常有用乔。只有一个细节。如果最终值不是最大值,则可以使用y [-1]。我添加了一条水平线来澄清。
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()
Plot with final y-axis value marked.
标记最终y轴值的绘图。