调整一些子图的水平间距(hspace)。

12

我有一个情节,我想让其中一个面板与其他四个面板分开。我希望其余的四个面板共享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为none

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

使用 f.subplots_adjust(hspace=0) 可以将所有子图连接在一起。有没有办法只连接底部的四个面板?

enter image description here

1个回答

34

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

以下是一个快速示例:

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()

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接