简体   繁体   中英

Rename legends in subplots: matplotlib

I have a data frame stats with two columns Old and New . I plot them in two separate subplots but I would like to rename my legends but leave subtitles as they are. plt.legend(labels= ['Stats']) renames only one subplot legend though. Will appreciate your help.

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 returns the plot axes or an array of axes for each subplot. Use the axes to change their legends.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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