笔记|matplotlib 技巧|使用 gricspec 实现的不同大小子图
matplotlib.gridspec.GridSpec
matplotlib 官方文檔:https://matplotlib.org/stable/api/_as_gen/matplotlib.gridspec.GridSpec.html#matplotlib.gridspec.GridSpec
matplotlib 源碼地址:https://github.com/matplotlib/matplotlib/blob/v3.7.1/lib/matplotlib/gridspec.py#L319-L472
參數(shù)列表
nrows, ncols : int —— 行數(shù)和列數(shù)
The number of rows and columns of the grid.
figure : Figure, optional —— 在指定 figure 中構(gòu)建
Only used for constrained layout to create a proper layoutgrid.
left, right, top, bottom : float, optional —— 上下左右邊界位置(用于設(shè)置邊距)
Extent of the subplots as a fraction of figure width or height. Left cannot be larger than right, and bottom cannot be larger than top. If not given, the values will be inferred from a figure or rcParams at draw time. See also GridSpec.get_subplot_params.
wspace : float, optional —— 子圖的橫向間距
The amount of width reserved for space between subplots, expressed as a fraction of the average axis width. If not given, the values will be inferred from a figure or rcParams when necessary. See also GridSpec.get_subplot_params.
hspace : float, optional —— 子圖的縱向間距
The amount of height reserved for space between subplots, expressed as a fraction of the average axis height. If not given, the values will be inferred from a figure or rcParams when necessary. See also GridSpec.get_subplot_params.
width_ratios : array-like of length ncols, optional —— 每一個(gè)子圖的寬度比值
Defines the relative widths of the columns. Each column gets a relative width of width_ratios[i] / sum(width_ratios). If not given, all columns will have the same width.
height_ratios : array-like of length nrows, optional —— 每一個(gè)子圖的高度比值
Defines the relative heights of the rows. Each row gets a relative height of height_ratios[i] / sum(height_ratios). If not given, all rows will have the same height.
樣例
我們使用繪制 “散點(diǎn)圖 + 雙軸直方圖” 的官方樣例來(lái)了解 gridspec 中各個(gè)參數(shù)的用途:
matplotlib 官方樣例:https://matplotlib.org/stable/gallery/lines_bars_and_markers/scatter_hist.html
import numpy as np import matplotlib.pyplot as plt# 構(gòu)造樣例數(shù)據(jù) x = np.random.randn(1000) y = x ** 2 - 3 + np.random.randn(1000)def scatter_hist(x, y, ax, ax_histx, ax_histy):# 關(guān)閉 x 軸直方圖的 x 軸刻度 + 關(guān)閉 y 軸直方圖的 y 軸刻度ax_histx.tick_params(axis="x", labelbottom=False)ax_histy.tick_params(axis="y", labelleft=False)# 在 ax 軸上繪制散點(diǎn)圖ax.scatter(x, y)# 計(jì)算直方圖中桶的邊界binwidth = 0.25xymax = max(np.max(np.abs(x)), np.max(np.abs(y)))lim = (int(xymax/binwidth) + 1) * binwidthbins = np.arange(-lim, lim + binwidth, binwidth)# 在 ax_histx 和 ax_histy 上繪制兩個(gè)直方圖ax_histx.hist(x, bins=bins)ax_histy.hist(y, bins=bins, orientation='horizontal')樣例 1:原官方案例
# 初始化正方形的 figure fig = plt.figure(figsize=(6, 6))# 構(gòu)造 gridspec 對(duì)象 gs = fig.add_gridspec(2, 2, width_ratios=(4, 1), height_ratios=(1, 4),left=0.1, right=0.9, bottom=0.1, top=0.9,wspace=0.05, hspace=0.05)# 構(gòu)造 3 個(gè)圖的 ax 對(duì)象 ax = fig.add_subplot(gs[1, 0]) ax_histx = fig.add_subplot(gs[0, 0], sharex=ax) ax_histy = fig.add_subplot(gs[1, 1], sharey=ax)# 調(diào)用畫(huà)圖方法 scatter_hist(x, y, ax, ax_histx, ax_histy)樣例 2:調(diào)整 left、right、bottom、top
將樣例 1 中構(gòu)造 gridspec 對(duì)象的語(yǔ)句修改如下:
gs = fig.add_gridspec(2, 2, width_ratios=(4, 1), height_ratios=(1, 4),left=0.0, right=1.0, bottom=0.0, top=1.0,wspace=0.05, hspace=0.05)可以看到,left、right、bottom、top 四個(gè)參數(shù)為子圖邊界相較于整個(gè)畫(huà)布的位置,畫(huà)布左側(cè)和上方邊界是 0,右側(cè)和下方邊界是 1。這個(gè)配置只考慮了子圖的邊界在畫(huà)布中的位置,而沒(méi)有考慮坐標(biāo)刻度的位置。
樣例 3:調(diào)整 width_ratios 和 height_ratios
將樣例 1 中構(gòu)造 gridspec 對(duì)象的語(yǔ)句修改如下:
gs = fig.add_gridspec(2, 2, width_ratios=(6, 1), height_ratios=(1, 6),left=0.1, right=0.9, bottom=0.1, top=0.9,wspace=0.05, hspace=0.05)因?yàn)槲覀冋{(diào)大了第一列和第二行的寬度,所以實(shí)際上也就縮小了第二列和第一行的寬表。
樣例 4:調(diào)整 wspace 和 hspace
將樣例 1 中構(gòu)造 gridspec 對(duì)象的語(yǔ)句修改如下:
gs = fig.add_gridspec(2, 2, width_ratios=(4, 1), height_ratios=(1, 4),left=0.1, right=0.9, bottom=0.1, top=0.9,wspace=1, hspace=1)可以看到,調(diào)大了 wspace 和 hsapce 后圖的間距增大,但 wspace 和 hspace 的單位與 left、right、bottom 和 top 是不一致的。
總結(jié)
以上是生活随笔為你收集整理的笔记|matplotlib 技巧|使用 gricspec 实现的不同大小子图的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 小动物立体定位架的全球与中国市场2022
- 下一篇: 框架的优缺点(TP CI)