Stream: ESDS

Topic: GridSpec


view this post on Zulip David Bailey (Dec 28 2023 at 20:23):

I have a silly question about GridSpec. I am trying to do a 3 panel plot with the third one in a second row, centered in the middle. When I do something like:

gs = GridSpec(2,2)

ax = fig.add_subplot(gs[0,0], ...)

ax = fig.add_subplot(gs[0,1], ...)

ax = fig.add_subplot(gs[1,:], ...)

The plot in the second row is right-aligned instead of centered. I can get it to work with GridSpec(3,3), but then I have a bunch of whitespace. What am I missing here?

Dave

view this post on Zulip Anissa Zacharias (Dec 28 2023 at 20:27):

Hello Dave,

I think you might actually want to try subplot_mosaic if you're open to abandoning GridSpec.

See this example that has a very similar layout to what you're describing

view this post on Zulip Anissa Zacharias (Dec 28 2023 at 20:30):

Here's a more general subplot_mosaic example , too . This is my preferred method to do anything with non-standard subplot layouts!

view this post on Zulip Fred Castruccio (Jan 02 2024 at 19:02):

Hi Dave,

I think this is what you want.

fig = plt.figure(tight_layout=True)
gs = gridspec.GridSpec(2, 4)
ax1 = fig.add_subplot(gs[0, :2])
ax2 = fig.add_subplot(gs[0, 2:])
ax3 = fig.add_subplot(gs[1, 1:3])

Cheers,
Fred


Last updated: May 16 2025 at 17:14 UTC