How to associate sharex=True and different hspace between plots

Hello,

In the following code, I want to add some vertical space between the top and middle plots, and keep zero space between middle and bottom plots.

With a sharex=True between all plots

Is there a way ? Any idea ?

Thanks,

Olivier

import numpy as np
import matplotlib.pyplot as plt, matplotlib as mpl
ii = np.arange(100)
fig, axs = plt.subplots (nrows=3, sharex=True, height_ratios=[1, 2, 3])
plt.subplots_adjust (hspace=0)
axs[0].plot (ii)
axs[1].plot (ii+2)
axs[2].plot (ii*2)

Hi. I can’t see anything particularly promising. You could try manually adjusting with

https://matplotlib.org/stable/gallery/subplots_axes_and_figures/subplots_adjust.html

Or you could try explicitly stating the binding positions of the subplots (see bottom chart in the example)

https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.set_position.html

Both run the risk of losing the sharex functionality. You could explicity label your axes though eg

ticklocs = (0, 1,2, 3,4,5, 6)
ticklabs = (‘a’,‘’,‘b’,‘’,‘c’)
ax0.set_ticklabels(ticks=ticklocs,labels=()) #empty labels to set tick markers with no labels
ax1.set_ticklabels(ticks=ticklocs,labels=())
ax2.set_ticklabels(ticks=ticklocs,labels=ticklabs) #not empty labels