On Tue, Jan 4, 2011 at 6:17 PM, Paul Ivanov <pivanov314@gmail.com> wrote:
Gf B, on 2011-01-04 12:31,  wrote:
> On Mon, Jan 3, 2011 at 3:53 PM, Paul Ivanov <pivanov314@gmail.com> wrote:
> Gf B, on 2011-01-03 15:23,  wrote:
> > > Can such a "grid of grids" be done with matplotlib?  If so, could someone
> > > show me how?
> >
> >  You'll be able to group the inner grids visually by adjusting the
> > spacing.  As far as getting the spines to only outline the outer
> > grid, and not the inner grid - I think you'll have to do it
> > manually by hiding the appropriate spines for the inner subplots.
> >
> This sort of ad-hoc manual tweaking is what I was hoping to avoid.
> What would it take to implement a "true" grid-of-grids function in
> matplotlib?  What I mean by this is a function that can arrange in a grid
> not only plots but also other grids.  (Is this a question for the devel
> group?)

I think the true grid-of-grids functunality is already
implemented. Here's a replication of your Mathematica plots:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from itertools import product

def squiggle_xy(a, b, c, d, i=np.linspace(0.0, 2*np.pi, 200)):
   return np.sin(i*a)*np.cos(i*b), np.sin(i*c)*np.cos(i*d)

f = plt.figure(figsize=(8, 8))

# gridspec inside gridspec
outer_grid = gridspec.GridSpec(4, 4, wspace=0.0, hspace=0.0)
for i in xrange(16):
   inner_grid = gridspec.GridSpecFromSubplotSpec(3, 3,
           subplot_spec=outer_grid[i], wspace=0.0, hspace=0.0)
   a, b = int(i/4)+1,i%4+1
   for j, (c, d) in enumerate(product(range(1, 4), repeat=2)):
       ax = plt.Subplot(f, inner_grid[j])
       ax.plot(*squiggle_xy(a, b, c, d))

all_axes = f.get_axes()

#show only the outside spines
for ax in all_axes:
   for sp in ax.spines.values():
   if ax.is_first_row():
   if ax.is_last_row():
   if ax.is_first_col():
   if ax.is_last_col():


It's a matter of taste, but I think you can get away hiding all
spines, and just setting the hspace and wspace for the outer_grid
to some small value (this is what I meant by 'adjusting the

I'll send a patch to the devel list shortly adding this example
with the following documentation

A Complex Nested GridSpec using SubplotSpec
Here's a more sophisticated example of nested gridspect where we put
a box around outer 4x4 grid, by hiding appropriate spines in each of the
inner 3x3 grids.

it'll be placed on the gridspec page, after this section:
Paul Ivanov
314 address only used for lists,  off-list direct email at:
http://pirsquared.org | GPG/PGP key id: 0x0F3E28F7

Just to add, because it is related, another tool that gives you advanced control over your axes is the AxesGrid toolkit:


However, gridspec should be exactly what you need for this particular problem.

Ben Root