可視化常見問題解決方案(五)子圖間距問題解決方案
1.前言
我們在日常畫圖的過程中經(jīng)常會發(fā)現(xiàn)子圖之間的距離過近導(dǎo)致下方子圖的標題被上方子圖的坐標軸遮擋望迎。如下圖所示:2.解決方案
2.1解決方案1
subplots_adjust(self, left=None, bottom=None, right=None, top=None,wspace=None, hspace=None)
參數(shù)1:left:指定子圖左邊緣距離
參數(shù)2:bottom:指定子圖底部距離
參數(shù)3:right:指定子圖右邊緣距離
參數(shù)4:top:指定子圖頂部距離
參數(shù)5:wspace:指定子圖之間的橫向距離
參數(shù)6:hspace:指定子圖之間的縱向距離
參數(shù)說明:
所有的距離并不是指絕對數(shù)值而是一個相對比例壕探,數(shù)值都在0-1之間洞翩。
left和bottom參數(shù)指的是左右兩邊的空白比例,例如left=0.5相當于左空白為0.5.而right和top參數(shù)則指得是整個圖框所占的比例囤踩,例如top=0.9相當于上空白為0.1债蓝。
對上圖的使用subplots_adjust方法進行調(diào)整,示例代碼如下:
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] # 設(shè)置支持中文
import numpy as np
#初始化顏色
color = ['red', 'black', 'blue', 'pink', 'purple', 'green', 'yellow', 'orange', '#d00F0d']
#初始化圖框
figure, [(ax1, ax2, ax3), (ax4, ax5, ax6), (ax7, ax8, ax9) ] = plt.subplots(3, 3)
#畫圖京髓,設(shè)置標簽,標題
for i, j in zip([ax1, ax2, ax3, ax4, ax5, ax6, ax7, ax8, ax9], np.arange(9)):
i.plot(np.random.random(10), color=color[j])
i.set_title('ax' + str(j+1), fontsize=25)
i.set_xlabel('X', fontsize=15)
i.set_ylabel('Y', fontsize=15)
figure.subplots_adjust(hspace=0.5) #使用subplots_adjust方法調(diào)整子圖參數(shù)
plt.show()
繪圖結(jié)果如下:
2.2解決方案2
如果認為使用subplots_adjust方法無法實時反饋效果或者對參數(shù)數(shù)值大小不好確定商架,那么也可以試試畫圖完畢之后使用maplotlib自帶的工具欄工具Configure subplots進行設(shè)置堰怨,其參數(shù)與subplots_adjust方法是相同的。
調(diào)整前圖片:
打開工具欄Configure subplots:
調(diào)整后圖片:
調(diào)整后
2.3解決方案3
除了上述兩種方法甸私,matplotlib庫還提供了一種更加智能的調(diào)整參數(shù)方法诚些,即使用tight_layout方法調(diào)整子圖之間和周圍的填充,是指產(chǎn)生指定的緊密布局皇型。該方法參數(shù)如下:
tight_layout( pad=1.08, h_pad=None, w_pad=None, rect=None)
參數(shù)1:pad:指定圖形邊緣和子圖邊緣之間的填充诬烹,以字體大小為單位
參數(shù)2:h_pad:指定相鄰子圖邊緣之間縱向的填充,以字體大小為單位
參數(shù)3:w_pad:指定相鄰子圖邊緣之間橫向的填充弃鸦,以字體大小為單位
參數(shù)4:rect:指定figure中子圖矩形圖框的位置
參數(shù)說明:
h_pad和w_pad參數(shù)的意義與subplots_adjust方法中的hspace和wspace是一致的绞吁,都是描述子圖之間的距離。
pad參數(shù)面數(shù)的是所有子圖組成的矩形和圖框之間的距離唬格。
當參數(shù)不合理的時候家破,將無法應(yīng)用tight_layout,會給出警告购岗,所以最好不要傳遞任何參數(shù)汰聋,以期達到最好的效果。
同樣對上圖的使用tight_layout方法進行調(diào)整喊积,示例代碼如下:
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] # 設(shè)置支持中文
import numpy as np
#初始化顏色
color = ['red', 'black', 'blue', 'pink', 'purple', 'green', 'yellow', 'orange', '#d00F0d']
#初始化圖框
figure, [(ax1, ax2, ax3), (ax4, ax5, ax6), (ax7, ax8, ax9) ] = plt.subplots(3, 3)
#畫圖烹困,設(shè)置標簽,標題
for i, j in zip([ax1, ax2, ax3, ax4, ax5, ax6, ax7, ax8, ax9], np.arange(9)):
i.plot(np.random.random(10), color=color[j])
i.set_title('ax' + str(j+1), fontsize=25)
i.set_xlabel('X', fontsize=15)
i.set_ylabel('Y', fontsize=15)
figure.tight_layout()
plt.show()
畫圖結(jié)果如下:
2.4解決方案4
最后一種方法是在初始化子圖的時候就是定參數(shù)constrained layout=True
乾吻,這樣就可以使用約束布局來調(diào)整繪圖元素的位置髓梅。類似于tight_layout拟蜻,但設(shè)計得更加靈活。示例代碼如下:
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] # 設(shè)置支持中文
import numpy as np
#初始化顏色
color = ['red', 'black', 'blue', 'pink', 'purple', 'green', 'yellow', 'orange', '#d00F0d']
#初始化圖框
figure, [(ax1, ax2, ax3), (ax4, ax5, ax6), (ax7, ax8, ax9) ] = plt.subplots(3, 3, constrained_layout=True) #使用約數(shù)布局
#畫圖枯饿,設(shè)置標簽酝锅,標題
for i, j in zip([ax1, ax2, ax3, ax4, ax5, ax6, ax7, ax8, ax9], np.arange(9)):
i.plot(np.random.random(10), color=color[j])
i.set_title('ax' + str(j+1), fontsize=25)
i.set_xlabel('X', fontsize=15)
i.set_ylabel('Y', fontsize=15)
plt.show()