我正在嘗試制作一個左側有 12 個圖的網格,右側有一個更大的圖的圖形。問題是 matplotlib 在小子圖之間增加了太多空間。我怎樣才能擺脫這個空間?
import matplotlib.pyplot as plt
fig = plt.figure(constrained_layout=True)
gs = fig.add_gridspec(4, 5)
ax0 = fig.add_subplot(gs[0, 0])
ax0.set_ylabel(r'$a_t$')
ax0.tick_params(labelbottom=False)
ax0.set_title(r'$\beta = 0$')
ax1 = fig.add_subplot(gs[1, 0], sharex=ax0)
ax1.set_ylabel(r'$x_t$')
ax1.tick_params(labelbottom=False)
ax = fig.add_subplot(gs[0, 1], sharex=ax0, sharey=ax0)
ax.tick_params(labelleft=False, labelbottom=False)
ax.set_title(r'$\beta = 0.5$')
ax = fig.add_subplot(gs[1, 1], sharex=ax0, sharey=ax1)
ax.tick_params(labelleft=False, labelbottom=False)
ax = fig.add_subplot(gs[0, 2], sharex=ax0, sharey=ax0)
ax.tick_params(labelleft=False, labelbottom=False)
ax.set_title(r'$\beta = 1$')
ax = fig.add_subplot(gs[1, 2], sharex=ax0, sharey=ax1)
ax.tick_params(labelleft=False, labelbottom=False)
ax = fig.add_subplot(gs[2, 0], sharex=ax0, sharey=ax0)
ax.set_ylabel(r'$a_t$')
ax.tick_params(labelbottom=False)
ax.set_title(r'$\beta = 1.5$')
ax = fig.add_subplot(gs[3, 0], sharex=ax0, sharey=ax1)
ax.set_ylabel(r'$x_t$')
ax = fig.add_subplot(gs[2, 1], sharex=ax0, sharey=ax0)
ax.tick_params(labelleft=False, labelbottom=False)
ax.set_title(r'$\beta = 2$')
ax = fig.add_subplot(gs[3, 1], sharex=ax0, sharey=ax1)
ax.tick_params(labelleft=False)
ax = fig.add_subplot(gs[2, 2], sharex=ax0, sharey=ax0)
ax.tick_params(labelleft=False, labelbottom=False)
ax.set_title(r'$\beta = 3$')
ax = fig.add_subplot(gs[3, 2], sharex=ax0, sharey=ax1)
ax.tick_params(labelleft=False)
ax = fig.add_subplot(gs[:, 3:])
這是它的樣子:

這是沒有最后一行 ( ) 的樣子ax = fig.add_subplot(gs[:, 3:]):

情節更加緊密。這就是我希望它看起來的樣子,但當然還有右邊的大情節。
謝謝!
uj5u.com熱心網友回復:
如果您想嘗試子圖,例如
fig0 = plt.figure(constrained_layout=True)
sfigs = fig0.subfigures(1, 2, width_ratios=[3, 2])
fig = sfigs[0]
gs = fig.add_gridspec(4, 3)
...
# code as before
...
ax = sfigs[1].subplots()
作業正常,但請注意頂部脊椎沒有對齊,因為右側子圖沒有標題

uj5u.com熱心網友回復:
一個好的解決方案似乎是
代碼:
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
fig = plt.figure(constrained_layout=True, figsize=(6, 3))
gs0 = fig.add_gridspec(1, 2, width_ratios=(3, 2))
gs = gridspec.GridSpecFromSubplotSpec(4, 3, subplot_spec=gs0[0])
ax0 = fig.add_subplot(gs[0, 0])
ax0.set_ylabel(r'$a_t$')
ax0.tick_params(labelbottom=False)
ax0.set_title(r'$\beta = 0$')
ax1 = fig.add_subplot(gs[1, 0], sharex=ax0)
ax1.set_ylabel(r'$x_t$')
ax1.tick_params(labelbottom=False)
ax = fig.add_subplot(gs[0, 1], sharex=ax0, sharey=ax0)
ax.tick_params(labelleft=False, labelbottom=False)
ax.set_title(r'$\beta = 0.5$')
ax = fig.add_subplot(gs[1, 1], sharex=ax0, sharey=ax1)
ax.tick_params(labelleft=False, labelbottom=False)
ax = fig.add_subplot(gs[0, 2], sharex=ax0, sharey=ax0)
ax.tick_params(labelleft=False, labelbottom=False)
ax.set_title(r'$\beta = 1$')
ax = fig.add_subplot(gs[1, 2], sharex=ax0, sharey=ax1)
ax.tick_params(labelleft=False, labelbottom=False)
ax = fig.add_subplot(gs[2, 0], sharex=ax0, sharey=ax0)
ax.set_ylabel(r'$a_t$')
ax.tick_params(labelbottom=False)
ax.set_title(r'$\beta = 1.5$')
ax = fig.add_subplot(gs[3, 0], sharex=ax0, sharey=ax1)
ax.set_ylabel(r'$x_t$')
ax = fig.add_subplot(gs[2, 1], sharex=ax0, sharey=ax0)
ax.tick_params(labelleft=False, labelbottom=False)
ax.set_title(r'$\beta = 2$')
ax = fig.add_subplot(gs[3, 1], sharex=ax0, sharey=ax1)
ax.tick_params(labelleft=False)
ax = fig.add_subplot(gs[2, 2], sharex=ax0, sharey=ax0)
ax.tick_params(labelleft=False, labelbottom=False)
ax.set_title(r'$\beta = 3$')
ax = fig.add_subplot(gs[3, 2], sharex=ax0, sharey=ax1)
ax.tick_params(labelleft=False)
gs = gridspec.GridSpecFromSubplotSpec(1, 1, subplot_spec=gs0[1])
ax = fig.add_subplot(gs[0, 0])
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/454512.html
標籤:Python matplotlib
