我收到此错误,请问怎么了?
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])
答案 0 :(得分:2)
尝试根据文档添加逗号
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])