Легенда - получить ярлык - PullRequest
1 голос
/ 16 марта 2019

Я получаю эту ошибку, что не так, пожалуйста?

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])

1 Ответ

2 голосов
/ 16 марта 2019

Попробуйте добавить запятую согласно документам
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])
...