在图 Python 中为子图提供不同的方向

Giving subplots different orientations within fig Python

你好,我正在尝试使用 matplotlib 中的 subplot 函数绘制 6 个图形,但是我想将这些图表分解为不同的可视化样式。我想让 3 行 a1 占据第一列的全部 a2, a3 占据第二列,c1 , c2, c3 占据第三列。

fig, (a1, a2,a3,c1,c2,c3) = plt.subplots(6)
fig, (a1, a2,a3,c1,c2,c3) = plt.subplots(6)
#Compounding Amount being plotted
a1.plot(x_indexes,Amount_list)
c1.plot(x_indexes, Non_compounding_list)
L =1
S = 1
x_long = []
x_short = []
for i in L_Amount_list:
    x_long.append(L)
    L+=1
for i in S_Amount_list:
    x_short.append(S)
    S+= 1
a2.plot(x_short, S_Amount_list)
a3.plot(x_long,L_Amount_list)
c2.plot(x_short,S_Non_compounding_list)
c3.plot(x_long,L_Non_compounding_list)

正如 BigBen 评论的那样,gridspec 可以解决问题。

import matplotlib.pyplot as plt   # v 3.3.2

fig = plt.figure(constrained_layout=True)
gs = fig.add_gridspec(6, 3)
a1 = fig.add_subplot(gs[:, 0])
a1.set_title('a1')
a2 = fig.add_subplot(gs[0:3, 1])
a2.set_title('a2')
a3 = fig.add_subplot(gs[3:6, 1])
a3.set_title('a2')
c1 = fig.add_subplot(gs[0:2, 2])
c1.set_title('c1')
c2 = fig.add_subplot(gs[2:4, 2])
c2.set_title('c2')
c3 = fig.add_subplot(gs[4:6, 2])
c3.set_title('c2')
plt.show()