Matplotlib: No handles with labels found to put in legend.

2018-10-24 10:03:10 +08:00
 songdg
plt.legend()提示 No handles with labels found to put in legend,不能正常显示 legend,不知哪里出问题,使用环境为 Spyder。
8305 次点击
所在节点    Python
2 条回复
xider
2018-10-24 10:12:46 +08:00
没有代码看不出问题
songdg
2018-10-24 10:30:39 +08:00
这是书上的一个例子,我发现不用 plt.show()就能显示,但是 plt.legend(loc=(0.64,0.1))就显示上面的提示。
import matplotlib.pyplot as plt

d = [11,12,13,14,15,16,17]

t0 = [15.3,12.6,12.7,13.2,12.3,11.4,12.8]
t1 = [26.1,26.2,24.3,25.1,26.7,27.8,26.9]
t2 = [22.3,20.6,19.8,21.6,21.3,19.4,21.4]

plt.plot(d,t0,label='Tokyo')
plt.plot(d,t1,label='Hawaii')
plt.plot(d,t2,label='Hong Kong')

plt.xlim(11,15)
plt.ylim(0,30)

plt.xlabel('Date',size=12,fontweight='semibold')
plt.ylabel('Temperature (°C)',size=12,fontweight='semibold')

plt.grid(True,linewidth=0.5,color='#aaaaaa',linestyle='-')

plt.title("Daily temperature of 3 cities in the second week of December", size=14, fontweight='bold')

plt.legend(loc=(0.64,0.1))
plt.show()

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/500528

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX