. Matplotlib】複数グラフの配置と大きさ (Gridspec) |
Matplotlib】複数グラフの配置と大きさ (Gridspec) |
Matplotlib】複数グラフの配置と大きさ (Gridspec) |

【Matplotlib】複数グラフの配置と大きさ (Gridspec)

ax1 = fig.add_subplot(gs[0, :]) : 1行目,列はすべて ax2 = fig.add_subplot(gs[1, :-1]) : 2行目,最後の列まで ax3 = fig.add_subplot(gs[1:, -1]) : 2行目以降, 最後の列 ax4 = fig.add_subplot(gs[2, 0]) : 3行目,1列目 ax5 = fig.add_subplot(gs[2, 1]) : 3行目,1列目

Figureの呼び出し GridSpecFromSubplotSpecで列ごとにグラフを配置する

GridSpecFromSubplotSpecは,GridSpecで作成した グリッドレイアウトを継承 することができます

GridSpecの 列成分だけ継承すると,列ごとにグラフ配置 ができます

GridSpecで 1行2列のグリッドレイアウト を作成 gs0 = gridspec.GridSpec(1, 2, figure=fig)

1列目をGridSpecFromSubplotSpecに継承し, 3行1列のグリッドレイアウト を作成 gs1 = gridspec.GridSpecFromSubplotSpec(3, 1, subplot_spec=gs0[0])

2列目をGridSpecFromSubplotSpecに継承し, 2行1列のグリッドレイアウト を作成 gs2 = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=gs0[1])

# step2 グラフフレームの作成 fig = plt.figure(constrained_layout=True) # 1行2列のグリッドレイアウト gs0 = gridspec.GridSpec(1, 2, figure=fig) # step3 グラフの描画 # 1列目をGridSpecFromSubplotSpecに継承し,3行1列のグリッドレイアウトを作成 gs1 = gridspec.GridSpecFromSubplotSpec(3, 1, subplot_spec=gs0[0]) for n in range(3): ax = fig.add_subplot(gs1[n]) ax.plot(x, y1, color='C'+str(n), label='ax'+str(n+1)) # 2列目をGridSpecFromSubplotSpecに継承し,2行1列のグリッドレイアウトを作成 gs2 = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=gs0[1]) for n in range(2): ax = fig.add_subplot(gs2[n]) ax.scatter(x, y1, color='C'+str(n+3), label='ax'+str(n+4))

参考文献

制約付きレイアウトでの軸のサイズ変更

GridSpec

GridSpecの使い方

GridSpecFromSubplotSpec

📎📎📎📎📎📎📎📎📎📎