I use a GridSpec within matplotlib to trying to generate the following plot:

However I fail at adding the titles at the desired positions, which are at the top center of each two columns. The following code creates the plot above sans titles:
fig = plt.figure(constrained_layout=True)
gs = fig.add_gridspec(2, 6)
for i in range(0, 6, 2):
fig.add_subplot(gs[:, i])
fig.add_subplot(gs[0, i + 1])
fig.add_subplot(gs[1, i + 1])
Adding the following two lines creates the titles but also creates a figure above the other figures:
title = fig.add_subplot(gs[:, i:i + 2])
title.set_title(f'title #{i}')
How do I have to change the given code to get the desired result depicted above? Is there a way to hide the new figures? Is there a way to set titles/text without figures?
One variant is to hide the newly added figures but their titles Adding this line to the proposed other two, the plot looks like desired:
title.set_axis_off()
Therefore the full script would look like this:
fig = plt.figure(constrained_layout=True)
gs = fig.add_gridspec(2, 6)
for i in range(0, 6, 2):
fig.add_subplot(gs[:, i])
fig.add_subplot(gs[0, i + 1])
fig.add_subplot(gs[1, i + 1])
title = fig.add_subplot(gs[:, i:i + 2])
title.set_title(f'title #{i}')
title.set_axis_off()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With