0

I'd like to set different horizontal space values between the rows of a subplot system. I tried with the function plt.subplots_adjust, but it changes the space between all the rows with a same common value. For example, I want that the space between the 1st, 2nd ant 3rd row is equal to hspace=0.05, meanwhile the space between the 3rd and the 4th row must be hspace=0.5. How I could do that?

Looking here on Stackoverflow I found only two posts here and here, but they didn't help me.

1 Answers1

1

It is easier to create two separate GridSpec for this. Some good options are also available here

As you mentioned, you need the first 3 subplots to have 0.05 hspace, while the next one (between 3rd and 4th) to have 0.5 hspace. So, the first Grdspec will have hspace of 0.05, while the next one will have 0.5 hspace. Also, I have removed the x-axis for the first group and have a shared x-axis. This is shown below in an example of 4 subplots example...

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

Plot

enter image description here

Redox
  • 9,321
  • 5
  • 9
  • 26