具有共享轴循环的Python子块

具有共享轴循环的Python子块,python,matplotlib,plot,Python,Matplotlib,Plot,我试图绘制一个子地块图,其中每个子地块由共享x轴的两个子地块组成。我尝试了以下代码: gs_top = plt.GridSpec(6, 3, hspace = 0.0001) gs_base = plt.GridSpec(6, 3, hspace = 0.95) f2 = plt.figure() for i in range(9): up_id = [0,1,2,6,7,8,12,13,15] bot_id = [3,4,5,9,10,11,15,16,17]

我试图绘制一个子地块图,其中每个子地块由共享x轴的两个子地块组成。我尝试了以下代码:

gs_top = plt.GridSpec(6, 3, hspace = 0.0001)
gs_base = plt.GridSpec(6, 3, hspace = 0.95)

f2 = plt.figure()

for i in range(9):
    up_id  = [0,1,2,6,7,8,12,13,15]
    bot_id = [3,4,5,9,10,11,15,16,17]

    axarr2 = f2.add_subplot(gs_top[up_id[i]])


    axarr2.plot()   

    ax_sub = f2.add_subplot(gs_base[bot_id[i]], sharex= axarr2) 
    ax_sub.imshow()
    axarr2.set_title('title')
    axarr2.xaxis.set_visible(False)

我应该如何在
plt.GridSpec()
中设置参数?

我想您应该使用
GridSpec.gridspecfromsublotspec
在GridSpec中创建另一个GridSpec。所以假设你想要一个3x3的网格,每个单元应该包含两个子单元,垂直连接在一起,共享它们的x轴

import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
gs = gridspec.GridSpec(3, 3, hspace=0.6,wspace=0.3)

for i in range(9):
    gss = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=gs[i],
                                           hspace=0.0)

    ax0 = fig.add_subplot(gss[0])
    ax1 = fig.add_subplot(gss[1], sharex=ax0)

    x = np.linspace(0,6*np.pi)
    y = np.sin(x)
    ax0.plot(x,y)
    ax1.plot(x/2,y)

    ax0.set_title('title {}'.format(i))
    ax0.tick_params(axis="x", labelbottom=0)

plt.show()

我猜您希望使用
gridspec.gridspecfromsublotspec
在gridspec中创建另一个gridspec。所以假设你想要一个3x3的网格,每个单元应该包含两个子单元,垂直连接在一起,共享它们的x轴

import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
gs = gridspec.GridSpec(3, 3, hspace=0.6,wspace=0.3)

for i in range(9):
    gss = gridspec.GridSpecFromSubplotSpec(2, 1, subplot_spec=gs[i],
                                           hspace=0.0)

    ax0 = fig.add_subplot(gss[0])
    ax1 = fig.add_subplot(gss[1], sharex=ax0)

    x = np.linspace(0,6*np.pi)
    y = np.sin(x)
    ax0.plot(x,y)
    ax1.plot(x/2,y)

    ax0.set_title('title {}'.format(i))
    ax0.tick_params(axis="x", labelbottom=0)

plt.show()