python matplotlib實現(xiàn)將圖例放在圖外
關(guān)于matplotlib如何設(shè)置圖例的位置?如何將圖例放在圖外?以及如何在一幅圖有多個子圖的情況下,刪除重復(fù)的圖例?我用一個簡單的例子說明一下。
import pandas as pdimport numpy as npimport matplotlib.pyplot as pltfig = plt.figure(1)ax1 = fig.add_subplot(2,2,1)ax2 = fig.add_subplot(2,2,2)ax3 = fig.add_subplot(2,2,3)ax4 = fig.add_subplot(2,2,4)df1 = pd.DataFrame(np.random.randn(3,5),columns = [’one’,’two’,’three’,’four’,’five’])df2 = pd.DataFrame(np.random.randn(3,5),columns = [’one’,’two’,’three’,’four’,’five’])df3 = pd.DataFrame(np.random.randn(3,5),columns = [’one’,’two’,’three’,’four’,’five’])df4 = pd.DataFrame(np.random.randn(3,5),columns = [’one’,’two’,’three’,’four’,’five’])df1.plot(ax = ax1, title = 'df1', grid = ’on’)df2.plot(ax = ax2, title = 'df1', grid = ’on’)df3.plot(ax = ax3, title = 'df1', grid = ’on’)df4.plot(ax = ax4, title = 'df1', grid = ’on’)plt.show()
運行結(jié)果如下
可以看出,隨機(jī)生成了幾個dataframe,在一個figure()中生成了四個子圖,每個子圖的圖例都是dataframe.columns里的值,那么如何移除這些圖例?
import pandas as pdimport numpy as npimport matplotlib.pyplot as pltfig = plt.figure(1)ax1 = fig.add_subplot(2,2,1)ax2 = fig.add_subplot(2,2,2)ax3 = fig.add_subplot(2,2,3)ax4 = fig.add_subplot(2,2,4)df1 = pd.DataFrame(np.random.randn(3,5),columns = [’one’,’two’,’three’,’four’,’five’])df2 = pd.DataFrame(np.random.randn(3,5),columns = [’one’,’two’,’three’,’four’,’five’])df3 = pd.DataFrame(np.random.randn(3,5),columns = [’one’,’two’,’three’,’four’,’five’])df4 = pd.DataFrame(np.random.randn(3,5),columns = [’one’,’two’,’three’,’four’,’five’])df1.plot(ax = ax1, title = 'df1', grid = ’on’)df2.plot(ax = ax2, title = 'df1', grid = ’on’)df3.plot(ax = ax3, title = 'df1', grid = ’on’)df4.plot(ax = ax4, title = 'df1', grid = ’on’)ax1.legend_.remove() ##移除子圖ax1中的圖例ax2.legend_.remove() ##移除子圖ax2中的圖例ax3.legend_.remove() ##移除子圖ax3中的圖例plt.show()
可以看出ax1,ax2,ax3中的圖例都被移除了,但是上圖還不是很美觀?有沒有什么辦法將圖例放到圖外面呢?請看:
import pandas as pdimport numpy as npimport matplotlib.pyplot as pltfig = plt.figure(1)ax1 = fig.add_subplot(2,2,1)ax2 = fig.add_subplot(2,2,2)ax3 = fig.add_subplot(2,2,3)ax4 = fig.add_subplot(2,2,4)df1 = pd.DataFrame(np.random.randn(3,5),columns = [’one’,’two’,’three’,’four’,’five’])df2 = pd.DataFrame(np.random.randn(3,5),columns = [’one’,’two’,’three’,’four’,’five’])df3 = pd.DataFrame(np.random.randn(3,5),columns = [’one’,’two’,’three’,’four’,’five’])df4 = pd.DataFrame(np.random.randn(3,5),columns = [’one’,’two’,’three’,’four’,’five’])df1.plot(ax = ax1, title = 'df1', grid = ’on’)df2.plot(ax = ax2, title = 'df1', grid = ’on’)df3.plot(ax = ax3, title = 'df1', grid = ’on’)df4.plot(ax = ax4, title = 'df1', grid = ’on’)ax1.legend_.remove()ax2.legend_.remove()ax3.legend_.remove()ax4.legend(loc=2, bbox_to_anchor=(1.05,1.0),borderaxespad = 0.) ##設(shè)置ax4中l(wèi)egend的位置,將其放在圖外plt.show()
其中參數(shù)loc用于設(shè)置legend的位置
bbox_to_anchor用于在bbox_transform坐標(biāo)(默認(rèn)軸坐標(biāo))中為圖例指定任意位置。
以上這篇python matplotlib實現(xiàn)將圖例放在圖外就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Intellij IDEA 2019 最新亂碼問題及解決必殺技(必看篇)2. 利用django創(chuàng)建一個簡易的博客網(wǎng)站的示例3. PHP5.0正式發(fā)布 不完全兼容PHP4 新增多項功能4. JS+css3實現(xiàn)幻燈片輪播圖5. ASP.NET MVC獲取多級類別組合下的產(chǎn)品6. 未來的J2EE主流應(yīng)用框架:對比Spring和EJB37. Android自定義View實現(xiàn)掃描效果8. 《javascript設(shè)計模式》學(xué)習(xí)筆記三:Javascript面向?qū)ο蟪绦蛟O(shè)計單例模式原理與實現(xiàn)方法分析9. JS繪圖Flot如何實現(xiàn)動態(tài)可刷新曲線圖10. 關(guān)于HTML5的img標(biāo)簽
