Specifying exactly zero inter-plot spacing in nested grids

Hi all,

I'm trying to get a grid of grids of plots, where the inner grids have
no inter-plot spacing. Is there a recommended way to go about this?
I'm aware AxesGrid can do the no-spacing requirement, but it's not so
obvious that it can be used to nest one grid within another, as
gridspec can. Can the two work together, or can I achieve what I need
with gridspec alone?
I've tried setting the gridspec hspace and wspace both to 0., but that
doesn't force the spacing to be exactly zero.

Thanks for any help,

Angus.

···

--
AJC McMorland
Post-doctoral research fellow
Neurobiology, University of Pittsburgh

Angus McMorland, on 2011-03-24 15:46, wrote:

Hi all,

I'm trying to get a grid of grids of plots, where the inner grids have
no inter-plot spacing. Is there a recommended way to go about this?
I'm aware AxesGrid can do the no-spacing requirement, but it's not so
obvious that it can be used to nest one grid within another, as
gridspec can. Can the two work together, or can I achieve what I need
with gridspec alone?
I've tried setting the gridspec hspace and wspace both to 0., but that
doesn't force the spacing to be exactly zero.

Hi Angus,

I think you can get the desired functionality with gridspec
alone. Take a look at
doc/users/plotting/examples/demo_gridspec06.py which you can find
here

https://github.com/matplotlib/matplotlib/blob/f1c8/doc/users/plotting/examples/demo_gridspec06.py

See also the thread called "How to make a grid of (plot) grids?" from
this list in January, 2011 at [1] or [2]:

1. http://old.nabble.com/How-to-make-a-grid-of-(plot)-grids--td30581281.html
2. Re: [Matplotlib-users] How to make a grid of (plot) grids?

best,

···

--
Paul Ivanov
314 address only used for lists, off-list direct email at:
http://pirsquared.org | GPG/PGP key id: 0x0F3E28F7

Thanks Paul,

I think you can get the desired functionality with gridspec
alone. Take a look at
doc/users/plotting/examples/demo_gridspec06.py which you can find
here

https://github.com/matplotlib/matplotlib/blob/f1c8/doc/users/plotting/examples/demo_gridspec06.py

I've played around with this example, and it seems like I'm not quite
there yet. I should add that I'm using imshow rather than plot here.
In this case, the inter-inner-grid spacing becomes dependent on the
figure aspect ratio. I presume this is because it is trying to
maintain (as it should) the aspect ratio of the images, but it would
be nice for it do this my manipulating only the outside margins, and
honour the wspace=0., hspace=0. as requested.

Here's a simplified version of the above script to illustrate my point:

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

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

# gridspec inside gridspec
outer_grid = gridspec.GridSpec(2, 3, wspace=0.0, hspace=0.0)

for i in xrange(6):
    inner_grid = gridspec.GridSpecFromSubplotSpec(3, 3,
            subplot_spec=outer_grid[i], wspace=0.0, hspace=0.0)
    for j, (c, d) in enumerate(product(range(1, 4), repeat=2)):
        ax = plt.Subplot(f, inner_grid[j])
        ax.imshow(np.ones((10,10)) * c * d, vmin=1, vmax=9)
        ax.set_xticks()
        ax.set_yticks()
        f.add_subplot(ax)

all_axes = f.get_axes()

#show only the outside spines
for ax in all_axes:
    for sp in ax.spines.values():
        sp.set_visible(False)
    if ax.is_first_row():
        ax.spines['top'].set_visible(True)
    if ax.is_last_row():
        ax.spines['bottom'].set_visible(True)
    if ax.is_first_col():
        ax.spines['left'].set_visible(True)
    if ax.is_last_col():
        ax.spines['right'].set_visible(True)

plt.show()

Of course, it is relatively trivial to calculate the correct figure
aspect ratio in this case, but after adding in other elements like
labels, this can become problematic. Is it possible to specify exactly
the inner spacing, and make the outer margins automatically adjusted
to get everything to fit nicely?

Thanks,

Angus

···

On 24 March 2011 19:23, Paul Ivanov <pivanov314@...287...> wrote:
--
AJC McMorland
Post-doctoral research fellow
Neurobiology, University of Pittsburgh