matplotlib库 UserWarning: Legend

2021-06-18  本文已影响0人  我的章鱼小丸子呢

场景

在使用 matplotlib.pyplot 画图添加图例:

fig, ax1 = plt.subplots()line1 = ax1.plot(x, y, color='firebrick')  # draw a lineax2.legend([line1], ['First']) 

错误信息

显示以下提示:


错误信息

原因与解决方案

原因在于,plot 返回 的 list 对象(list of Line2D)需要解构,因此需要在line1和等号之间加一个逗号:

fig, ax1 = plt.subplots()line1, = ax1.plot(x, y, color='firebrick')  # draw a lineax2.legend([line1], ['First'])
上一篇 下一篇

猜你喜欢

热点阅读