jruota
jruota

Reputation: 147

Multiple GridSpecs in one Figure

I was working through matplotlib's documentation (http://matplotlib.org/users/gridspec.html#adjust-gridspec-layout), and in this particular example I do not understand the logic behind the layout of two GridSpecs in one figure. The significant part of the code they use (leaving out text, titles and labels) is

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

f = plt.figure()

gs1 = GridSpec(3, 3)
gs1.update(left=0.05, right=0.48, wspace=0.05)
ax1 = plt.subplot(gs1[:-1, :])
ax2 = plt.subplot(gs1[-1, :-1])
ax3 = plt.subplot(gs1[-1, -1])

gs2 = GridSpec(3, 3)
gs2.update(left=0.55, right=0.98, hspace=0.05)
ax4 = plt.subplot(gs2[:, :-1])
ax5 = plt.subplot(gs2[:-1, -1])
ax6 = plt.subplot(gs2[-1, -1])

This gives the following result (http://matplotlib.org/_images/demo_gridspec03.png): enter image description here

These two GridSpecs seem to be aligned next to each other by default. Do I miss something in the code, that does this explicitly?

I tried to add a third GridSpec, like so:

gs3 = gridspec.GridSpec(3, 3)
ax7 = plt.subplot(gs3[:, 0])
ax8 = plt.subplot(gs3[:, 1:])

but this just fills the whole figure and the first two GridSpecs are "overpainted".

To restate my question, is there some implicit logic for the layout of two GridSpecs in a figure (note that I know of the method GridSpecFromSubplotSpec, but here it is not being used)?

Upvotes: 0

Views: 1011

Answers (1)

Thomas Kühn
Thomas Kühn

Reputation: 9810

The GridSpec extent can be adjusted with the update command. With this line you limit the first GridSpec to the left side (48%) of the Figure.

gs1.update(left=0.05, right=0.48, wspace=0.05)

The second GridSpec is then limited to the right side of the Figure with

gs1.update(left=0.55, right=0.98, hspace=0.05)

Similarly you can limit the vertical extent with the keywords top and bottom.

Upvotes: 2

Related Questions