【问题标题】:Pylab - Adjust hspace for some of the subplotsPylab - 为一些子图调整 hspace
【发布时间】:2015-05-03 10:39:20
【问题描述】:

我有一个情节,我想让一个面板与其他四个面板分开。我希望四个面板的其余部分共享 x 轴。该图如下所示。我希望底部的四个面板共享 x 轴。我试过了

f = plt.figure()
ax6=f.add_subplot(511)
ax4=f.add_subplot(515)
ax1=f.add_subplot(512,sharex=ax4)
ax2=f.add_subplot(513,sharex=ax4)
ax3=f.add_subplot(514,sharex=ax4)

但是,这对我不起作用。附图是用

制作的
f = plt.figure()
ax6=f.add_subplot(511)
ax4=f.add_subplot(515)
ax1=f.add_subplot(512)
ax2=f.add_subplot(513)
ax3=f.add_subplot(514)

然后将 xticks 设置为无

ax1.get_xaxis().set_ticklabels([])
ax2.get_xaxis().set_ticklabels([])
ax3.get_xaxis().set_ticklabels([])

使用 f.subplots_adjust(hspace=0) 连接所有子图。有没有办法只加入底部的四个面板?

谢谢!

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    为此使用两个单独的gridspec objects 是最简单的。这样,您可以为不同的子图组设置独立的边距、填充等。

    举个简单的例子:

    import numpy as np
    import matplotlib.pyplot as plt
    
    # We'll use two separate gridspecs to have different margins, hspace, etc
    gs_top = plt.GridSpec(5, 1, top=0.95)
    gs_base = plt.GridSpec(5, 1, hspace=0)
    fig = plt.figure()
    
    # Top (unshared) axes
    topax = fig.add_subplot(gs_top[0,:])
    topax.plot(np.random.normal(0, 1, 1000).cumsum())
    
    # The four shared axes
    ax = fig.add_subplot(gs_base[1,:]) # Need to create the first one to share...
    other_axes = [fig.add_subplot(gs_base[i,:], sharex=ax) for i in range(2, 5)]
    bottom_axes = [ax] + other_axes
    
    # Hide shared x-tick labels
    for ax in bottom_axes[:-1]:
        plt.setp(ax.get_xticklabels(), visible=False)
    
    # Plot variable amounts of data to demonstrate shared axes
    for ax in bottom_axes:
        data = np.random.normal(0, 1, np.random.randint(10, 500)).cumsum()
        ax.plot(data)
        ax.margins(0.05)
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-19
      • 2013-08-14
      • 1970-01-01
      相关资源
      最近更新 更多