Arranging subplots on a square regular grid

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Arranging subplots on a square regular grid

Daniele Nicolodi
Hello,

I would like to arrange a number of square subplots (as in subplots with
square axes as obtained with `ax.axis('equal')`) in a grid such that the
distance between rows and columns is exactly the same.

I can use a GridSpec but, as soon as I ask for the axes to be square,
the distance between axes changes. Is there a way to enforce the equal
distances constrain (namely stretch the padding between the axes and the
margin of the figure but do not stretch the padding between axes) or the
only solution is to do it manually?

Thank you in advance.

Best,
Dan
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Arranging subplots on a square regular grid

Joe Kington
If you're okay with a fixed figure size, you can use plt.figaspect to set this up fairly easily: (i.e. this will be incorrect as soon as you resize the plot window)

import numpy as np
import matplotlib.pyplot as plt

nrows, ncols = 8, 12
dx, dy = 1, 2
figsize = plt.figaspect(float(dy * nrows) / float(dx * ncols))

fig, axes = plt.subplots(nrows, ncols, figsize=figsize)
for ax in axes.flat:
    data = np.random.random((10*dy, 10*dx))
    ax.imshow(data, interpolation='none', cmap='gray')
    ax.set(xticks=[], yticks=[])

pad = 0.05 # Padding around the edge of the figure
xpad, ypad = dx * pad, dy * pad
fig.subplots_adjust(left=xpad, right=1-xpad, top=1-ypad, bottom=ypad)

plt.show()
(From here, for reference: https://stackoverflow.com/questions/32633322/changing-aspect-ratio-of-subplots-in-matplotlib/32635933#32635933 )
If you need the spacing to remain constant as the figure size changes, it's possible, but more complex.  You may find it's easiest to use draw callbacks in that case.
Hope that helps,
-Joe

On Tue, Oct 30, 2018 at 4:44 PM Daniele Nicolodi <[hidden email]> wrote:
Hello,

I would like to arrange a number of square subplots (as in subplots with
square axes as obtained with `ax.axis('equal')`) in a grid such that the
distance between rows and columns is exactly the same.

I can use a GridSpec but, as soon as I ask for the axes to be square,
the distance between axes changes. Is there a way to enforce the equal
distances constrain (namely stretch the padding between the axes and the
margin of the figure but do not stretch the padding between axes) or the
only solution is to do it manually?

Thank you in advance.

Best,
Dan
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Arranging subplots on a square regular grid

Jody Klymak
Hi Daniele,

This is also explicitly the use-case that `axes_grid1` toolkit is meant for.   I’m trying to move as much axes_grid1 stuff out of the toolkit into the main code as possible, but what you want is orthogonal to how layout managers that are currently in matplotlib work, so axes_grid1 is still useful here. 


Cheers,  Jody

On 31 Oct 2018, at 08:30, Joe Kington <[hidden email]> wrote:

If you're okay with a fixed figure size, you can use plt.figaspect to set this up fairly easily: (i.e. this will be incorrect as soon as you resize the plot window)

import numpy as np
import matplotlib.pyplot as plt

nrows, ncols = 8, 12
dx, dy = 1, 2
figsize = plt.figaspect(float(dy * nrows) / float(dx * ncols))

fig, axes = plt.subplots(nrows, ncols, figsize=figsize)
for ax in axes.flat:
    data = np.random.random((10*dy, 10*dx))
    ax.imshow(data, interpolation='none', cmap='gray')
    ax.set(xticks=[], yticks=[])

pad = 0.05 # Padding around the edge of the figure
xpad, ypad = dx * pad, dy * pad
fig.subplots_adjust(left=xpad, right=1-xpad, top=1-ypad, bottom=ypad)

plt.show()
(From here, for reference: https://stackoverflow.com/questions/32633322/changing-aspect-ratio-of-subplots-in-matplotlib/32635933#32635933 )
If you need the spacing to remain constant as the figure size changes, it's possible, but more complex.  You may find it's easiest to use draw callbacks in that case.
Hope that helps,
-Joe

On Tue, Oct 30, 2018 at 4:44 PM Daniele Nicolodi <[hidden email]> wrote:
Hello,

I would like to arrange a number of square subplots (as in subplots with
square axes as obtained with `ax.axis('equal')`) in a grid such that the
distance between rows and columns is exactly the same.

I can use a GridSpec but, as soon as I ask for the axes to be square,
the distance between axes changes. Is there a way to enforce the equal
distances constrain (namely stretch the padding between the axes and the
margin of the figure but do not stretch the padding between axes) or the
only solution is to do it manually?

Thank you in advance.

Best,
Dan
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users

--
Jody Klymak    






_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Arranging subplots on a square regular grid

Daniele Nicolodi
On 31-10-2018 09:38, Jody Klymak wrote:
> This is also explicitly the use-case that `axes_grid1` toolkit is meant
> for.   I’m trying to move as much axes_grid1 stuff out of the toolkit
> into the main code as possible, but what you want is orthogonal to how
> layout managers that are currently in matplotlib work, so axes_grid1 is
> still useful here. 
>
> https://matplotlib.org/gallery/axes_grid1/simple_axesgrid.html

Hi Jody,

thank you very much. That's exactly what I was looking for!  I don't
know how I missed it in the documentation.  It would be very nice if
this could expose an API similar to the GridSpec one, but I don't know
how feasible this is.

Thanks,
Dan
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users