簡體   English   中英

圖例-獲取標簽

[英]Legend - get label

我收到此錯誤,請問怎么了?

plt.legend(handles=[d1, d2])
  File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 3553, in legend
    ret = gca().legend(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/matplotlib/axes/_axes.py", line 501, in legend
    labels = [handle.get_label() for handle in handles]
  File "/usr/lib/python3/dist-packages/matplotlib/axes/_axes.py", line 501, in <listcomp>
    labels = [handle.get_label() for handle in handles]
AttributeError: 'list' object has no attribute 'get_label'

代碼的一部分是

d1 = plt.plot(xdata, ydata1, "sb", markersize = 5, ls = "solid", label = 'name1', linestyle = 'dashed')
d2 = plt.plot(xdata, ydata2, "vr", markersize = 5, ls = "solid", label = 'name2', linestyle = 'dashed')

plt.legend(handles=[d1, d2])

嘗試根據文檔添加逗號
https://matplotlib.org/tutorials/intermediate/legend_guide.html#legend-handlers

d1, = plt.plot(xdata, ydata1, "sb", markersize = 5, ls = "solid", label = 'name1', linestyle = 'dashed')
d2, = plt.plot(xdata, ydata2, "vr", markersize = 5, ls = "solid", label = 'name2', linestyle = 'dashed')

plt.legend(handles=[d1, d2])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM