I am trying to create a figure in pyplot with specific gridspec, but I got into a dead end.
When using subfigures it is needed to use constrained_layout = True
option in the figure definition, but it disables the hspace = 0
option in the GridSpec definition.
An example looks like this:
import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
fig = plt.figure(
figsize = (16,9),
constrained_layout = True
)
fig.suptitle("fig suptitle")
subfigs = fig.subfigures(1,3)
for sf in subfigs:
sf.suptitle("subfig suptitle")
gs = GridSpec(6,1,sf,
hspace = 0
)
ax1 = sf.add_subplot(gs[0:2,0])
ax1.scatter([1,1,5,2,7,5], [1,5,8,2,6,8])
ax1.set_title("ax title")
for i in range(3,6):
ax = sf.add_subplot(gs[i,0],
sharex = ax1
)
if i == 3:
ax.set_title("ax title")
ax.plot([6,2,5,5,6,8,7])
# ax.set_title("ax title")
When I comment the constrained_layout = True
line the plots are in the right possitions, but fig suptitle disappears.
Is there any workaround that keeps the fig suptitle shown and leaves hspace = 0
?