gpt4 book ai didi

python - 图例 - 获取标签

转载 作者:行者123 更新时间:2023-12-01 01:07:31 30 4
gpt4 key购买 nike

我收到此错误,请问出了什么问题?

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

关于python - 图例 - 获取标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55197376/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com