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条答案
按热度按时间a1o7rhls1#
为此,创建两个单独的GridSpec更容易。
正如你提到的,你需要前3个子图有0.05 hspace,而下一个(第3和第4之间)有0.5 hspace。所以,第一个Grdspec将有0.05的hspace,而下一个将有0.5 hspace。此外,我已经删除了第一组的x轴,并有一个共享的x轴。这是在下面的4个子图的例子中显示的例子...
剧情