如何在matplotlib(python)中标记一条线?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17941083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to label a line in matplotlib (python)?
提问by Sibbs Gambling
I followed the documentation but still failed to label a line.
我遵循了文档,但仍然未能标记一条线。
plt.plot([min(np.array(positions)[:,0]), max(np.array(positions)[:,0])], [0,0], color='k', label='East') # West-East
plt.plot([0,0], [min(np.array(positions)[:,1]), max(np.array(positions)[:,1])], color='k', label='North') # South-North
In the code snippet above, I am trying to plot out the North direction and the East direction.
在上面的代码片段中,我试图绘制出北向和东向。
position
contains the points to be plotted.
position
包含要绘制的点。
But I end up with 2 straight lines with NO labelsas follows:
但我最终得到 2 条没有标签的直线,如下所示:
Where went wrong?
哪里出错了?
采纳答案by shaunakde
The argument label
is used to set the sting that will be shown in the legend. For example consider the following snippet:
该参数label
用于设置将在图例中显示的刺痛。例如,考虑以下代码段:
import matplotlib.pyplot as plt
plt.plot([1,2,3],'r-',label='Sample Label Red')
plt.plot([0.5,2,3.5],'b-',label='Sample Label Blue')
plt.legend()
plt.show()
This will plot 2 lines as shown:
这将绘制 2 条线,如图所示:
The arrow function supports labels. Do check this link: http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.arrow
箭头函数支持标签。请检查此链接:http: //matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.arrow