国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

python matplotlib實現將圖例放在圖外

瀏覽:56日期:2022-07-29 15:55:52

關于matplotlib如何設置圖例的位置?如何將圖例放在圖外?以及如何在一幅圖有多個子圖的情況下,刪除重復的圖例?我用一個簡單的例子說明一下。

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()

運行結果如下

python matplotlib實現將圖例放在圖外

可以看出,隨機生成了幾個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()

python matplotlib實現將圖例放在圖外

可以看出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.) ##設置ax4中legend的位置,將其放在圖外plt.show()

python matplotlib實現將圖例放在圖外

其中參數loc用于設置legend的位置

bbox_to_anchor用于在bbox_transform坐標(默認軸坐標)中為圖例指定任意位置。

以上這篇python matplotlib實現將圖例放在圖外就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 元谋县| 始兴县| 集贤县| 且末县| 湘乡市| 礼泉县| 溆浦县| 大庆市| 鄂尔多斯市| 长宁县| 建始县| 黔西| 阿巴嘎旗| 莱阳市| 平原县| 嵊州市| 平乐县| 拜泉县| 抚顺市| 灵寿县| 南投县| 榆社县| 鄂州市| 炎陵县| 绥滨县| 闽清县| 卢龙县| 安庆市| 临夏市| 观塘区| 信宜市| 平果县| 天水市| 突泉县| 九台市| 繁昌县| 泾川县| 茶陵县| 赫章县| 藁城市| 长武县|