matplotlib 多子图图例显示

错误示例

'''
plt.subplots方法使用返回值,再对返回的多个ax使用索引来使用每一个ax
这样记忆:plots为复数——>返回fig axes
'''
fig,axes = plt.subplots(1,2)
axes[0].plot([1, 2, 3, 4], [10, 20, 25, 30], color='lightblue', linewidth=3)
axes[0].set_xlim(0.5, 4.5)

axes[1].plot([10, 20, 25, 30],[1, 2, 3, 4] , color='lightblue', linewidth=3)
axes[1].set_xlim(1, 50)
plt.legend('hello')
plt.show();


图例hello显示不完全
图例未显示完全
正确

fig,axes = plt.subplots(1,2)
axes[0].plot([1, 2, 3, 4], [10, 20, 25, 30], color='lightblue', linewidth=3)
axes[0].set_xlim(0.5, 4.5)

axes[1].plot([10, 20, 25, 30],[1, 2, 3, 4] , color='lightblue', label='hello',linewidth=3)
axes[1].set_xlim(1, 50)
plt.legend()
plt.show();

如此即可显示完整

猜你喜欢

转载自blog.csdn.net/Hoshea_H/article/details/110360084