import matplotlib.pyplot as plt
# Use two gridspecs to have specific hspace
gs_top = plt.GridSpec(nrows=4, ncols=1, hspace=0.05) ## HSPACE for top 3
gs_base = plt.GridSpec(nrows=4, ncols=1, hspace=0.5) ##HSPACE for last one
fig = plt.figure()
# The first 3 shared axes
ax = fig.add_subplot(gs_top[0,:]) # Create the first one, then add others...
other_axes = [fig.add_subplot(gs_top[i,:], sharex=ax) for i in range(1, 3)]
bottom_axes = [ax] + other_axes
# Hide shared x-tick labels
for ax in bottom_axes[:-1]:
plt.setp(ax.get_xticklabels(), visible=False)
# Plot data
for ax in bottom_axes:
data = np.random.normal(0, 1, np.random.randint(10, 500)).cumsum()
ax.plot(data)
ax.margins(0.05)
#Finally plot the last one, with hspace of 0.5 - gs_base
finalax = fig.add_subplot(gs_base[-1,:])
finalax.plot(np.random.normal(0, 1, 1000).cumsum())
plt.show()
1条答案
按热度按时间7kqas0il1#
为此创建两个单独的GridSpec会更容易。还有一些不错的选择here
正如你提到的,你需要前3个子图有0.05 hspace,而下一个(第3和第4之间)有0.5 hspace。因此,第一个Grdspec的hspace为0.05,而下一个Grdspec的hspace为0.5。此外,我已经删除了第一组的x轴,并有一个共享的x轴。这在下面的4个子图示例中示出...
剧情