Retain square layout with GridSpec

183 views Asked by At

How do I align the GridSpec to look like this?

|‾ ‾ ‾ ‾|  |‾ ‾|  |‾ ‾|
|       |  |_ _|  |_ _|
|       |  |‾ ‾|  |‾ ‾|
|_ _ _ _|  |_ _|  |_ _|

I have tried the following:

import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec
import numpy as np

gs = GridSpec(2, 3, hspace=-0.1)
fig = plt.figure()
ax1 = fig.add_subplot(gs[:2, :2])
ax2 = fig.add_subplot(gs[0, 2])
ax3 = fig.add_subplot(gs[1, 2])

ax1.set_aspect("equal")
ax2.set_aspect("equal")
ax3.set_aspect("equal")
 
plt.show()

output:

enter image description here

But when adjusting the width and height of the plot manually, the spacing is not retained:

enter image description here

Is it possible to specify this constraint somehow?

1

There are 1 answers

0
Davide_sd On

After reading this tutorial page, this is the closest I could get:

import matplotlib.pyplot as plt
from matplotlib.gridspec import GridSpec

fig = plt.figure(constrained_layout=True)
gs0 = fig.add_gridspec(1, 2)

gs00 = gs0[0].subgridspec(1, 1)
ax0 = fig.add_subplot(gs00[0, 0])

gs01 = gs0[1].subgridspec(2, 2)
ax1 = fig.add_subplot(gs01[0, 0])
ax2 = fig.add_subplot(gs01[0, 1])
ax3 = fig.add_subplot(gs01[1, 0])
ax4 = fig.add_subplot(gs01[1, 1])
fig

enter image description here