如何在matplotlib(python)中标记一行?

时间:2013-07-30 07:43:27

标签: python matplotlib

我按照文档进行操作但仍无法标记一行。

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

在上面的代码片段中,我试图绘制出北方向和东方向。

position包含要绘制的点。

但我最终选择了两条没有标签的直线,如下所示: enter image description here

出了什么问题?

2 个答案:

答案 0 :(得分:13)

参数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()

这将绘制2条线,如图所示: Plot With 2 lines

箭头功能支持标签。请检查此链接: http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.arrow

答案 1 :(得分:1)

添加label属性时,不要忘记添加.legend()方法。

import matplotlib.pyplot as plt
plt.plot([1,2],[3,5],'ro',label='one')
plt.plot([1,2],[1,2],'g^',label='two')
plt.plot([1,2],[1,6],'bs',label='three')
plt.axis([0,4,0,10])
plt.ylabel('x2')
plt.xlabel('x1')
plt.legend()
plt.show()

enter image description here