Nigu
Nigu

Reputation: 2135

Matplotlib: Gridspec and aspect='auto' not compatible?

I want 4 exactly squared subplots contained within a square figure

I have the following minimal code which is unable to produce what I want.

#latexify()

g1 = gridspec.GridSpec(2, 2,height_ratios=[1,1],width_ratios=[1,1])

g1.update(wspace=0.05, hspace=0.4) # set the spacing between axes

f, ((ax0, ax1), (ax2, ax3)) = plt.subplots(2, 2, sharex='col', sharey='row')

ax0 = subplot(g1[0])
ax1 = subplot(g1[1])
ax2 = subplot(g1[2])
ax3 = subplot(g1[3])

ax0.plot([1, 2, 3], [1, 2, 3])
ax1.plot([1, 2, 3], [1, 2, 3])
ax2.plot([1, 2, 3], [1, 2, 3])
ax3.plot([1, 2, 3], [1, 2, 3])

ax0.set_title("G/G")
ax1.set_title("G/BN")
ax2.set_title("BN/BN (0$\degree$)")
ax3.set_title("BN/BN (60$\degree$)")

ax0.set_xticklabels([])
ax1.set_xticklabels([])
ax1.set_yticklabels([])
ax3.set_yticklabels([])

ax0.set(adjustable='box-forced', aspect='equal')
ax1.set(adjustable='box-forced', aspect='equal')
ax2.set(adjustable='box-forced', aspect='equal')
ax3.set(adjustable='box-forced', aspect='equal')

Toy plot

I get a lot of white space between the left and right subplots. I tried several options from other questions: turn of sharex/sharey, forcing different figure sizes (that's what the commented-out latexify() function allows me to do), play with the gridspec heigh_ratios, but to no avail.

Upvotes: 1

Views: 408

Answers (1)

Suever
Suever

Reputation: 65430

You need to set the figure size so that it's actually square.

f.set_size_inches((5,5))

enter image description here

Alternately, you can specify the figsize option in your subplots call

f, ((ax0, ax1), (ax2, ax3)) = plt.subplots(2, 2, sharex='col', 
                                           sharey='row', figsize=(5,5))

Upvotes: 1

Related Questions