代码之家  ›  专栏  ›  技术社区  ›  sponce

python中带有gridspec.gridspec的变量wspace

  •  0
  • sponce  · 技术社区  · 6 年前

    我想在matplotlib中使用GridSpec创建一个变量(两个不同的)wspace。

    Goal

    到目前为止我使用的是:

    gs1 = gridspec.GridSpec(6, 3, width_ratios=[1.5,1,1])
    gs1.update(wspace=0.4, hspace=0.3)
    ax1 = fig.add_subplot(gs1[0:2,0])
    ax2 = fig.add_subplot(gs1[2:4,0])
    ax3 = fig.add_subplot(gs1[4:6,0])
    ax4 = fig.add_subplot(gs1[0:3,1])
    ax5 = fig.add_subplot(gs1[3:6,1])
    ax6 = fig.add_subplot(gs1[0:3,2])
    ax7 = fig.add_subplot(gs1[3:6,2])
    

    你知道如何在我那令人惊叹的手绘中获得两个绿色突出的不同空间吗?

    山姆

    1 回复  |  直到 6 年前
        1
  •  2
  •   ImportanceOfBeingErnest    6 年前

    可以使用两个gridspec,一个包含一列和三行,另一个包含两行和两列。然后,您可以让第一个只扩展到图形的一半以下,然后从图形宽度的一半开始第二个。左参数和右参数之间的差异是间距。

    import matplotlib.pyplot as plt
    from matplotlib.gridspec import GridSpec
    
    fig = plt.figure()
    gs1 = GridSpec(3, 1, right=0.4)
    gs2 = GridSpec(2, 2, left=0.5)
    
    
    ax1 = fig.add_subplot(gs1[0,0])
    ax2 = fig.add_subplot(gs1[1,0])
    ax3 = fig.add_subplot(gs1[2,0])
    ax4 = fig.add_subplot(gs2[0,0])
    ax5 = fig.add_subplot(gs2[0,1])
    ax6 = fig.add_subplot(gs2[1,0])
    ax7 = fig.add_subplot(gs2[1,1])
    
    plt.show()
    

    enter image description here

    首先定义一个包含两列的“外部”gridspec,然后在每个列中放置一个内部gridspec,就可以实现这一点。

    import matplotlib.pyplot as plt
    from matplotlib.gridspec import GridSpec, GridSpecFromSubplotSpec
    
    fig = plt.figure()
    gs = GridSpec(1, 2, width_ratios=[1.5,2], wspace=0.3)
    
    gs1 = GridSpecFromSubplotSpec(3, 1, subplot_spec=gs[0])
    gs2 = GridSpecFromSubplotSpec(2, 2, subplot_spec=gs[1])
    
    ax1 = fig.add_subplot(gs1[0,0])
    ax2 = fig.add_subplot(gs1[1,0])
    ax3 = fig.add_subplot(gs1[2,0])
    ax4 = fig.add_subplot(gs2[0,0])
    ax5 = fig.add_subplot(gs2[0,1])
    ax6 = fig.add_subplot(gs2[1,0])
    ax7 = fig.add_subplot(gs2[1,1])
    
    plt.show()