Matplotlib自定義圖例(多張圖共享一個圖例)
1、應(yīng)用場景
可視化不同方法在各種超參數(shù)(或者不同數(shù)據(jù)集)的性能時,若用多個子圖形式可視化,則圖太小啦;若每個子圖弄成單獨的figure,則每個張圖都有一個圖例顯得非常冗余,如圖1所示。

圖1 每張圖都有一個圖例(非常冗余,并且有的數(shù)據(jù)被遮擋)
2、期望達到的效果,如圖2所示
解決方案:兩個子圖都不需要設(shè)置圖例,圖例單獨用一個figure來顯示(自定義圖例)

圖2 期望的效果
3、自定義圖例matplotlib代碼實現(xiàn)
方法一:利用Patch以及Line2D自定義圖例
- 缺點:暫時只能定義直線的圖的圖例;散點圖的圖例不行;
- 優(yōu)點:運行該代碼后,F(xiàn)igure 1就是我們想要的圖例圖。
import matplotlib.pyplot as plt
import matplotlib.lines as lines
import matplotlib.patches as mpatches
# 自定義圖例標記
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常見參數(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 水平圖例的個數(shù)
"""
ax.axis('off') # 去掉坐標的刻度
plt.show()方法一可視化效果,如圖3所示

圖3 方法一可視化效果
方法二:利用plt自定義圖例
- 缺點:會顯示2個Figure,其中Figure 2才是我們想要的圖例圖;
- 優(yōu)點:和我們以前畫圖的方式一樣(更加簡單),并且能夠構(gòu)建散點圖的圖例;
import matplotlib.pyplot as plt
# plt.plot()返回值后面要加一個逗號,不然會報錯;若使用Line2D就不需要逗號
# 注意:range(10)是隨機取的值,這里只需要它的圖例圖。
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) # 若只需要顯示部分曲線/散點圖的圖例時
ax.axis('off') # 去掉坐標的刻度
plt.show()方法二可視化效果,如圖4所示

圖4 方法三的可視化效果,其中Figure 2就是我們需要的圖
方法三:混合實驗方法一和方法二
import matplotlib.pyplot as plt
import matplotlib.lines as lines
import matplotlib.patches as mpatches
# 自定義圖例標記
line1 = lines.Line2D([0], [0], label='line1', marker='o', lw=2, c='green')
# 注意line2后面的逗號
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常見參數(shù)設(shè)置
axes:圖框; handle:圖例的每個元素; text:圖例每個元素對應(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 水平圖例的個數(shù)
"""
# plt.scatter
ax.axis('off') # 去掉坐標的刻度
plt.show()方法三的可視化效果如圖5所示

圖5 方法三可視化修改,其中Figure 2就是我們需要的圖例圖
4、總結(jié)
# 導入對應(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,注意別丟逗號 line2, = plt.plot([0], [0], label='line2', marker='X', lw=2, c='blue') b. 定義矩形圖例 path1 = mpatches.Patch(color='red', label='patch') c. 定義散點圖圖例 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”) # 去掉坐標刻度
5、用visio將單張的圖片合并在一起,并且手動構(gòu)建圖例,圖例的樣式更人性化。
到此這篇關(guān)于Matplotlib自定義圖例(多張圖共享一個圖例)的文章就介紹到這了,更多相關(guān)Matplotlib自定義圖例內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python把列表中的字符串轉(zhuǎn)成整型的3種方法詳解
這篇文章主要介紹了python把列表中的字符串轉(zhuǎn)成整型的3種方法詳解,python中在不同類型數(shù)據(jù)轉(zhuǎn)換方面是有標準庫的,使用非常方便,但是在開發(fā)中,經(jīng)常在list中字符轉(zhuǎn)成整形的數(shù)據(jù)方便遇到問題,需要的朋友可以參考下2023-07-07
使用Python的Treq on Twisted來進行HTTP壓力測試
這篇文章主要介紹了使用Python的Treq on Twisted來進行HTTP壓力測試,基于Python中的Twisted框架,需要的朋友可以參考下2015-04-04
python?實現(xiàn)?pymysql?數(shù)據(jù)庫操作方法
這篇文章主要介紹了python實現(xiàn)pymysql數(shù)據(jù)庫操作方法,文章基于python的相關(guān)內(nèi)容展開對?pymysql?數(shù)據(jù)庫操作方法的詳細介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-04-04
Python如何通過Flask-Mail發(fā)送電子郵件
這篇文章主要介紹了Python如何通過Flask-Mail發(fā)送電子郵件,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-01-01

