繁体   English   中英

重命名子图中的图例:matplotlib

[英]Rename legends in subplots: matplotlib

我有一个包含OldNew两列的数据框stats 我将它们绘制在两个单独的子图中,但我想重命名我的图例,但保留字幕原样。 plt.legend(labels= ['Stats'])只重命名一个子图图例。 将感谢您的帮助。

stats.plot(kind='bar',
               grid=False, subplots=True, 
               figsize=(20,10), fontsize=16, 
               color='#2E9240', 
               sharex=True, sharey=True)

plt.tick_params(
    axis='x',          # changes apply to the x-axis
    which='both',      # both major and minor ticks are affected
    bottom='off',      # ticks along the bottom edge are off
    top='off',         # ticks along the top edge are off
    labelbottom='off') # labels along the bottom edge are off

plt.legend(labels= ['Stats'])

plt.show()

在此处输入图片说明

DataFrame.plot返回每个子图的绘图轴或轴数组。 使用轴来更改它们的图例。

axes = stats.plot(kind='bar',
                  grid=False, subplots=True, 
                  figsize=(20,10), fontsize=16, 
                  color='#2E9240', 
                  sharex=True, sharey=True)
for ax in axes:
    ax.legend(['Stats'])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM