python matplotlib實(shí)現(xiàn)將圖例放在圖外
關(guān)于matplotlib如何設(shè)置圖例的位置?如何將圖例放在圖外?以及如何在一幅圖有多個(gè)子圖的情況下,刪除重復(fù)的圖例?我用一個(gè)簡(jiǎn)單的例子說(shuō)明一下。
import pandas as pd import numpy as np import matplotlib.pyplot as plt fig = 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()
運(yùn)行結(jié)果如下
可以看出,隨機(jī)生成了幾個(gè)dataframe,在一個(gè)figure()中生成了四個(gè)子圖,每個(gè)子圖的圖例都是dataframe.columns里的值,那么如何移除這些圖例?
import pandas as pd import numpy as np import matplotlib.pyplot as plt fig = 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中的圖例都被移除了,但是上圖還不是很美觀?有沒(méi)有什么辦法將圖例放到圖外面呢?請(qǐng)看:
import pandas as pd import numpy as np import matplotlib.pyplot as plt fig = 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實(shí)現(xiàn)將圖例放在圖外就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python 爬蟲(chóng) 實(shí)現(xiàn)增量去重和定時(shí)爬取實(shí)例
今天小編就為大家分享一篇python 爬蟲(chóng) 實(shí)現(xiàn)增量去重和定時(shí)爬取實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02Pytorch 使用CNN圖像分類(lèi)的實(shí)現(xiàn)
這篇文章主要介紹了Pytorch 使用CNN圖像分類(lèi)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Python獲取一個(gè)用戶名的組ID過(guò)程解析
這篇文章主要介紹了Python獲取一個(gè)用戶名的組ID過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09Python使用mmap實(shí)現(xiàn)內(nèi)存映射文件操作
內(nèi)存映射通??梢蕴岣逫/O的性能,本文主要介紹了Python使用mmap實(shí)現(xiàn)內(nèi)存映射文件操作,分享給大家,感興趣的可以了解一下2021-06-06對(duì)Python 語(yǔ)音識(shí)別框架詳解
今天小編就為大家分享一篇對(duì)Python 語(yǔ)音識(shí)別框架詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12Python 實(shí)現(xiàn)OpenCV格式和PIL.Image格式互轉(zhuǎn)
今天小編就為大家分享一篇Python 實(shí)現(xiàn)OpenCV格式和PIL.Image格式互轉(zhuǎn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01淺談PySpark SQL 相關(guān)知識(shí)介紹
這篇文章主要介紹了淺談PySpark SQL 相關(guān)知識(shí)介紹,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06