Matplotlib自定義圖例(多張圖共享一個(gè)圖例)
1、應(yīng)用場(chǎng)景
可視化不同方法在各種超參數(shù)(或者不同數(shù)據(jù)集)的性能時(shí),若用多個(gè)子圖形式可視化,則圖太小啦;若每個(gè)子圖弄成單獨(dú)的figure,則每個(gè)張圖都有一個(gè)圖例顯得非常冗余,如圖1所示。
圖1 每張圖都有一個(gè)圖例(非常冗余,并且有的數(shù)據(jù)被遮擋)
2、期望達(dá)到的效果,如圖2所示
解決方案:兩個(gè)子圖都不需要設(shè)置圖例,圖例單獨(dú)用一個(gè)figure來(lái)顯示(自定義圖例)
圖2 期望的效果
3、自定義圖例matplotlib代碼實(shí)現(xiàn)
方法一:利用Patch以及Line2D自定義圖例
- 缺點(diǎn):暫時(shí)只能定義直線的圖的圖例;散點(diǎn)圖的圖例不行;
- 優(yōu)點(diǎn):運(yùn)行該代碼后,F(xiàn)igure 1就是我們想要的圖例圖。
import matplotlib.pyplot as plt import matplotlib.lines as lines import matplotlib.patches as mpatches # 自定義圖例標(biāo)記 line1 = lines.Line2D([0], [0], label='line1', marker='o', lw=2, c='green') line2 = lines.Line2D([0], [0], label='line2', marker='X', lw=2, c='blue') patch1 = mpatches.Patch(color='purple', label='patch1') patch2 = mpatches.Patch(color='red', label='patch2') # 構(gòu)造圖例 # plt.figure() figsize: default: [6.4, 4.8] handles = [line1, line2, patch1, patch2] # 注意根據(jù)圖例的行數(shù)調(diào)整figsize的高度(i.e., 0.32) fig, ax = plt.subplots(figsize=(6.4, 0.32)) ax.legend(handles=handles, mode='expand', ncol=4, borderaxespad=0) """ legend常見(jiàn)參數(shù)設(shè)置 borderpad:控制上下邊距(default=0.4) borderaxespad:控制legend與圖邊框的距離(default=0.5) handlelength: handle與legend邊框的距離(default=2) handletextpad: handle與text之間的距離(default=0.8) mode='expand', 水平展示圖例 frameon=False: 不要圖例邊框 edgecolor: 圖例邊框顏色 fontsize: 圖例字體大小 ncol=4 水平圖例的個(gè)數(shù) """ ax.axis('off') # 去掉坐標(biāo)的刻度 plt.show()
方法一可視化效果,如圖3所示
圖3 方法一可視化效果
方法二:利用plt自定義圖例
- 缺點(diǎn):會(huì)顯示2個(gè)Figure,其中Figure 2才是我們想要的圖例圖;
- 優(yōu)點(diǎn):和我們以前畫(huà)圖的方式一樣(更加簡(jiǎn)單),并且能夠構(gòu)建散點(diǎn)圖的圖例;
import matplotlib.pyplot as plt # plt.plot()返回值后面要加一個(gè)逗號(hào),不然會(huì)報(bào)錯(cuò);若使用Line2D就不需要逗號(hào) # 注意:range(10)是隨機(jī)取的值,這里只需要它的圖例圖。 l1, = plt.plot(range(10), label='line1', marker='o', lw=2, c='green') l2, = plt.plot(range(10), label='line2', marker='X', lw=2, c='blue') s1 = plt.scatter(range(10), range(10), label='scatter1', marker='+') s2 = plt.scatter(range(10), range(10), label='scatter2', marker='+') fig, ax = plt.subplots(figsize=(6.4, 0.32)) ax.legend(handles=[l1, l2, s1, s2], labels=list('abcd'), mode='expand', ncol=4, borderaxespad=0) # ax.legend(handles=[l1, s1], labels=list('ab'), mode='expand', ncol=4, borderaxespad=0) # 若只需要顯示部分曲線/散點(diǎn)圖的圖例時(shí) ax.axis('off') # 去掉坐標(biāo)的刻度 plt.show()
方法二可視化效果,如圖4所示
圖4 方法三的可視化效果,其中Figure 2就是我們需要的圖
方法三:混合實(shí)驗(yàn)方法一和方法二
import matplotlib.pyplot as plt import matplotlib.lines as lines import matplotlib.patches as mpatches # 自定義圖例標(biāo)記 line1 = lines.Line2D([0], [0], label='line1', marker='o', lw=2, c='green') # 注意line2后面的逗號(hào) line2, = plt.plot([0], [0], label='line2', marker='X', lw=2, c='blue') s = plt.scatter([0], [0], label='scatter', marker='+') patch1 = mpatches.Patch(color='purple', label='patch1') # patch2 = mpatches.Patch(color='red', label='patch2') # 構(gòu)造圖例 # plt.figure() figsize: default: [6.4, 4.8] handles = [line1, line2, patch1, s] # 注意根據(jù)圖例的行數(shù)調(diào)整figsize的寬(i.e., 0.32) fig, ax = plt.subplots(figsize=(6.4, 0.32)) ax.legend(handles=handles, mode='expand', ncol=4, borderaxespad=0) """ legend常見(jiàn)參數(shù)設(shè)置 axes:圖框; handle:圖例的每個(gè)元素; text:圖例每個(gè)元素對(duì)應(yīng)name borderpad:控制上下邊距(default=0.4) borderaxespad:控制legend與圖邊框的距離(default=0.5) handlelength: handle與legend邊框的距離(default=2) handletextpad: handle與text之間的距離(default=0.8) mode='expand', 水平展示圖例 frameon=False: 不要圖例邊框 edgecolor: 圖例邊框顏色 fontsize: 圖例字體大小 ncol=4 水平圖例的個(gè)數(shù) """ # plt.scatter ax.axis('off') # 去掉坐標(biāo)的刻度 plt.show()
方法三的可視化效果如圖5所示
圖5 方法三可視化修改,其中Figure 2就是我們需要的圖例圖
4、總結(jié)
# 導(dǎo)入對(duì)應(yīng)的模塊 from matplotlib import lines import matplotlib.pyplot as plt from matplotlib import patches a. 定義線圖例 # 方法1 line1 = lines.Line2D([0], [0], label='line1', marker='o', lw=2, c='green') # 方法2,注意別丟逗號(hào) line2, = plt.plot([0], [0], label='line2', marker='X', lw=2, c='blue') b. 定義矩形圖例 path1 = mpatches.Patch(color='red', label='patch') c. 定義散點(diǎn)圖圖例 s1 = plt.scatter([0], [0], label='scatter', marker='+') d. 顯示圖例 handles = [line1, path1, line2, s1] fig, ax = plt.subplots(figsize=(6.4, 0.32)) # 根據(jù)行數(shù)更改0.32 ax.legend(handles=handles, mode='expand', ncol=4, borderaxespad=0) ax.axis(“off”) # 去掉坐標(biāo)刻度
5、用visio將單張的圖片合并在一起,并且手動(dòng)構(gòu)建圖例,圖例的樣式更人性化。
到此這篇關(guān)于Matplotlib自定義圖例(多張圖共享一個(gè)圖例)的文章就介紹到這了,更多相關(guān)Matplotlib自定義圖例內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python把列表中的字符串轉(zhuǎn)成整型的3種方法詳解
這篇文章主要介紹了python把列表中的字符串轉(zhuǎn)成整型的3種方法詳解,python中在不同類(lèi)型數(shù)據(jù)轉(zhuǎn)換方面是有標(biāo)準(zhǔn)庫(kù)的,使用非常方便,但是在開(kāi)發(fā)中,經(jīng)常在list中字符轉(zhuǎn)成整形的數(shù)據(jù)方便遇到問(wèn)題,需要的朋友可以參考下2023-07-07使用Python的Treq on Twisted來(lái)進(jìn)行HTTP壓力測(cè)試
這篇文章主要介紹了使用Python的Treq on Twisted來(lái)進(jìn)行HTTP壓力測(cè)試,基于Python中的Twisted框架,需要的朋友可以參考下2015-04-04Python爬蟲(chóng)獲取豆瓣電影并寫(xiě)入excel
這篇文章主要介紹了Python爬蟲(chóng)獲取豆瓣電影并寫(xiě)入excel ,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07python?實(shí)現(xiàn)?pymysql?數(shù)據(jù)庫(kù)操作方法
這篇文章主要介紹了python實(shí)現(xiàn)pymysql數(shù)據(jù)庫(kù)操作方法,文章基于python的相關(guān)內(nèi)容展開(kāi)對(duì)?pymysql?數(shù)據(jù)庫(kù)操作方法的詳細(xì)介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-04-04Python如何通過(guò)Flask-Mail發(fā)送電子郵件
這篇文章主要介紹了Python如何通過(guò)Flask-Mail發(fā)送電子郵件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01python 標(biāo)準(zhǔn)差計(jì)算的實(shí)現(xiàn)(std)
這篇文章主要介紹了python 標(biāo)準(zhǔn)差計(jì)算的實(shí)現(xiàn)(std),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07