簡體   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