0

My question is based on this question: Adjust hspace for some of the subplots

Which adjusts the top plot of a number of subplots and increases the difference in hspace. I want to increase the hspace between two plots within the subplots (in my case: between plot 3 and plot4 from the top).

Here is my example:

import numpy as np
import matplotlib.pyplot as plt

noise = np.random.rand(300)


gs_top = plt.GridSpec(9, 1, hspace=0.5)
gs_base = plt.GridSpec(9, 1, hspace=0)


fig = plt.figure()
fig.patch.set_facecolor('white')

ax0 = fig.add_subplot(gs_base[0,:])
ax1 = fig.add_subplot(gs_base[1,:])
ax2 = fig.add_subplot(gs_top[2,:])

ax3 = fig.add_subplot(gs_base[3,:])
ax4 = fig.add_subplot(gs_base[4,:])
ax5 = fig.add_subplot(gs_base[5,:])


ax0.plot(noise)
ax1.plot(noise)
ax2.plot(noise)
ax3.plot(noise)
ax4.plot(noise)
ax5.plot(noise)

In the example it is shown that the hspace increases between plot 3 and 4. However, I don't want to increase the space between plot 2 and plot 3. How can I adjust the hspace variable only on one side?

benschbob91
  • 155
  • 12

1 Answers1

0

Found the answer after manipulating google by asking with various word combinations. Found this: Stackoverflow answer

In short (dirty way): Adding a seperate axis and make it invisible. Example:

import numpy as np
import matplotlib.pyplot as plt

noise = np.random.rand(300)


gs_base = plt.GridSpec(7, 1, hspace=0, height_ratios=[1, 1, 1, 0.8, 1,1,1])


fig = plt.figure()
fig.patch.set_facecolor('white')

ax0 = fig.add_subplot(gs_base[0,:])
ax1 = fig.add_subplot(gs_base[1,:])
ax2 = fig.add_subplot(gs_base[2,:])

ax3 = fig.add_subplot(gs_base[3,:])
ax3.set_visible(False)
ax4 = fig.add_subplot(gs_base[4,:])
ax5 = fig.add_subplot(gs_base[5,:])
ax6 = fig.add_subplot(gs_base[6,:])


ax0.plot(noise)
ax1.plot(noise)
ax2.plot(noise)
ax4.plot(noise)
ax5.plot(noise)
ax6.plot(noise)

In long (correct way):

Couldn't figure it out for the moment.

benschbob91
  • 155
  • 12
  • That’s a fine way. You may also want to try the new subfigure functionality: https://matplotlib.org/stable/gallery/subplots_axes_and_figures/subfigures.html?highlight=subfigure – Jody Klymak May 31 '21 at 14:39