笔记|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
參數列表
nrows, ncols : int —— 行數和列數
The number of rows and columns of the grid.
figure : Figure, optional —— 在指定 figure 中構建
Only used for constrained layout to create a proper layoutgrid.
left, right, top, bottom : float, optional —— 上下左右邊界位置(用于設置邊距)
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 —— 每一個子圖的寬度比值
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 —— 每一個子圖的高度比值
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.
樣例
我們使用繪制 “散點圖 + 雙軸直方圖” 的官方樣例來了解 gridspec 中各個參數的用途:
matplotlib 官方樣例:https://matplotlib.org/stable/gallery/lines_bars_and_markers/scatter_hist.html
import numpy as np import matplotlib.pyplot as plt# 構造樣例數據 x = np.random.randn(1000) y = x ** 2 - 3 + np.random.randn(1000)def scatter_hist(x, y, ax, ax_histx, ax_histy):# 關閉 x 軸直方圖的 x 軸刻度 + 關閉 y 軸直方圖的 y 軸刻度ax_histx.tick_params(axis="x", labelbottom=False)ax_histy.tick_params(axis="y", labelleft=False)# 在 ax 軸上繪制散點圖ax.scatter(x, y)# 計算直方圖中桶的邊界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 上繪制兩個直方圖ax_histx.hist(x, bins=bins)ax_histy.hist(y, bins=bins, orientation='horizontal')樣例 1:原官方案例
# 初始化正方形的 figure fig = plt.figure(figsize=(6, 6))# 構造 gridspec 對象 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)# 構造 3 個圖的 ax 對象 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)# 調用畫圖方法 scatter_hist(x, y, ax, ax_histx, ax_histy)樣例 2:調整 left、right、bottom、top
將樣例 1 中構造 gridspec 對象的語句修改如下:
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 四個參數為子圖邊界相較于整個畫布的位置,畫布左側和上方邊界是 0,右側和下方邊界是 1。這個配置只考慮了子圖的邊界在畫布中的位置,而沒有考慮坐標刻度的位置。
樣例 3:調整 width_ratios 和 height_ratios
將樣例 1 中構造 gridspec 對象的語句修改如下:
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)因為我們調大了第一列和第二行的寬度,所以實際上也就縮小了第二列和第一行的寬表。
樣例 4:調整 wspace 和 hspace
將樣例 1 中構造 gridspec 對象的語句修改如下:
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)可以看到,調大了 wspace 和 hsapce 后圖的間距增大,但 wspace 和 hspace 的單位與 left、right、bottom 和 top 是不一致的。
總結
以上是生活随笔為你收集整理的笔记|matplotlib 技巧|使用 gricspec 实现的不同大小子图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小动物立体定位架的全球与中国市场2022
- 下一篇: 框架的优缺点(TP CI)