簡體   English   中英

在 Matplotlib (Python) 中處理子圖的比例

[英]Handling proportion of subplots in Matplotlib (Python)

嗨,我正在嘗試使用 matplotlib 創建下面的子圖。

子圖

我有以下代碼,但我似乎無法使用參數正確配置圖。如果對此有任何幫助,我們將不勝感激。 歡迎使用 python 上的任何其他繪圖工具,它們也可以幫助我以這種方式縫合 4 個繪圖。

非常感謝!

gs1 = fig9.add_gridspec(nrows=8, ncols=8, top=0.6, bottom=0.1,left = 0, right = 0.65,
                        wspace=0.05, hspace=0.05)
# f9_ax1 = fig9.add_subplot(gs1[:-1, :])
ax2 = fig9.add_subplot(gs1[:1, :1])
ax3 = fig9.add_subplot(gs1[:1, 1:])

gs2 = fig9.add_gridspec(nrows=4, ncols=4, top=1.2, bottom=0.4, left = 0, right = 0.5,
                        wspace=0.05, hspace=0.05)
ax4 = fig9.add_subplot(gs1[1: , :1])
ax5 = fig9.add_subplot(gs1[1:, 1:])

上面的代碼給出了這個在此處輸入圖片說明

例如,您可以將圖形划分為20 x 20網格,這意味着一個單元格5% x 5%圖形的5% x 5% 將您的比例35/65 -> 7/1340/60 -> 8/1250/50 -> 10/10縮放到此網格給出:

import matplotlib.pyplot as plt

fig = plt.figure(constrained_layout=True)
gs1 = fig.add_gridspec(nrows=20, ncols=20)
 
ax1 = fig.add_subplot(gs1[0:12, 0:7])     # top left     (size: 12x7  - 60x35)
ax2 = fig.add_subplot(gs1[0:12, 7:20])    # top right    (size: 12x13 - 60x65)
ax3 = fig.add_subplot(gs1[12:20, 0:10])   # bottom left  (size: 8x10  - 40x50)
ax4 = fig.add_subplot(gs1[12:20, 10:20])  # bottom right (size: 8x10  - 40x50)

網格規格

另請注意constrained_layout關鍵字,將其設置為True縮小子圖以使所有軸標簽可見,這可能會產生輕微改變縱橫比的不良影響。 將其設置為False ,可以更好地保留比例。 然而,目前約束布局是實驗性的,可能會更改或刪除。

另請參閱文檔以獲取更多信息。

創建 2 個單獨的 gridspecs,您可以在其中將兩者的height_ratios設置為(6, 4) ,然后根據需要為它們提供不同的width_ratios

例如:

import matplotlib.pyplot as plt

fig = plt.figure()

gs1 = fig.add_gridspec(nrows=2, ncols=2, hspace=0.05, wspace=0.05,
                       height_ratios=(6, 4), width_ratios=(35, 65))
gs2 = fig.add_gridspec(nrows=2, ncols=2, hspace=0.05, wspace=0.05, 
                       height_ratios=(6, 4), width_ratios=(1, 1))

ax1 = fig.add_subplot(gs1[0, 0])
ax2 = fig.add_subplot(gs1[0, 1])
ax3 = fig.add_subplot(gs2[1, 0])
ax4 = fig.add_subplot(gs2[1, 1])

plt.show()

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM