Possible to get variable spacing between certain subplots?

Hello matplotlib-users,

I’m using subplots to make an array of plots, but because some of the plots have wider y-axis tick labels than others, some of the subplots end up looking too close to each other. Here’s an image that shows what I mean:

http://web.mit.edu/wgrover/www/spacing.png

I’m currently using pylab.subplots_adjust(hspace = __, vspace = __) to adjust the subplot spacing, but since that applies to all subplots, no one setting looks right for the entire array of plots. Is there any way to set the spacing so that the subplots plus tick labels are evenly distributed? Or can I manually specify the spacing between each subplot? Thanks,

–Will

It is possible, but it is probably would be an incredible amount of work (unless someone knows of some nifty trick that I am not aware of). My suggestion to make things appear more “even” is to adjust the position of the ylabels in the second column to that it takes up more room between the first two columns.

The hard way would be to manually specify the extents in the “position” kwarg [left, bottom, width, height] for the constructor of the axes object. The values for those four parts would be in the coordinate system of the figure object.

I hope that is helpful,
Ben Root

···

On Thu, Oct 28, 2010 at 1:18 PM, Will Grover <wgrover@…1166…> wrote:

Hello matplotlib-users,

I’m using subplots to make an array of plots, but because some of the plots have wider y-axis tick labels than others, some of the subplots end up looking too close to each other. Here’s an image that shows what I mean:

http://web.mit.edu/wgrover/www/spacing.png

I’m currently using pylab.subplots_adjust(hspace = __, vspace = __) to adjust the subplot spacing, but since that applies to all subplots, no one setting looks right for the entire array of plots. Is there any way to set the spacing so that the subplots plus tick labels are evenly distributed? Or can I manually specify the spacing between each subplot? Thanks,

–Will

2010/10/28 Benjamin Root <ben.root@...1304...>:

Hello matplotlib-users,
I'm using subplots to make an array of plots, but because some of the
plots have wider y-axis tick labels than others, some of the subplots end up
looking too close to each other. Here's an image that shows what I mean:
http://web.mit.edu/wgrover/www/spacing.png
I'm currently using pylab.subplots_adjust(hspace = __, vspace = __) to
adjust the subplot spacing, but since that applies to all subplots, no one
setting looks right for the entire array of plots. Is there any way to set
the spacing so that the subplots *plus tick labels* are evenly distributed?
Or can I manually specify the spacing between each subplot? Thanks,

It is *possible*, but it is probably would be an incredible amount of work
(unless someone knows of some nifty trick that I am not aware of). My
suggestion to make things appear more "even" is to adjust the position of
the ylabels in the second column to that it takes up more room between the
first two columns.

The hard way would be to manually specify the extents in the "position"
kwarg [left, bottom, width, height] for the constructor of the axes object.
The values for those four parts would be in the coordinate system of the
figure object.

And this would not be super-hard. Opposed to the automatical
solution. I'm not familiar with pyplot etc., only with the API, but I
looked it up for you:
http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.axes

Notice that the tuple specifies the plot region, i.e. the
"decorations" like labels and tick labels are not included and will be
placed around.

You can also play with the Axes returned by the axes() call. I.e. you
can shift them around, using:
http://matplotlib.sourceforge.net/api/axes_api.html#matplotlib.axes.Axes.set_position.

Friedrich

···

On Thu, Oct 28, 2010 at 1:18 PM, Will Grover <wgrover@...1166...> wrote:

A belated thank you to Ben and Friedrich for your suggestions! I’m getting good results shifting subplots around using Axes.set_position() as Friedrich suggested, but I’ve noticed that whenever I shift a subplot into the region where another subplot would be, the subplot I’m shifting just disappears. This code demonstrates this; it creates a figure of six subplots and attempts to shift subplots 5 and 6 to the right:

import pylab

offset = 0.04 # works for 0.04, but subplot 5 disappears at >0.05

pylab.subplot(2,3,1)

pylab.subplot(2,3,2)

pylab.subplot(2,3,3)

pylab.subplot(2,3,4)

pylab.subplot(2,3,5)

bbox=pylab.gca().get_position()

pylab.gca().set_position([bbox.x0 + offset, bbox.y0, bbox.x1-bbox.x0, bbox.y1-bbox.y0])

pylab.subplot(2,3,6)

bbox=pylab.gca().get_position()

pylab.gca().set_position([bbox.x0 + offset, bbox.y0, bbox.x1-bbox.x0, bbox.y1-bbox.y0])

pylab.savefig(“out.png”)

Setting offset to 0.04 results in subplots 5 and 6 being shifted to the right a bit, as expected (see 0_04_offset.png), but if offset >= 0.05 subplot 5 disappears (see 0_05_offset.png). The funny thing is that you can keep increasing offset and subplot 6 keeps moving (see 0_20_offset.png), so it seems that you can use Axes.set_position() to move subplots anywhere except into the space formerly occupied by another subplot. Is this correct? Is there any way around this or am I using the wrong tool for the job? Thanks again for your help,

–Will

0_04_offset.png

0_05_offset.png

0_20_offset.png

···

On Sat, Oct 30, 2010 at 9:12 AM, Friedrich Romstedt <friedrichromstedt@…287…> wrote:

2010/10/28 Benjamin Root <ben.root@…1304…>:

On Thu, Oct 28, 2010 at 1:18 PM, Will Grover <wgrover@…1166…> wrote:

Hello matplotlib-users,

I’m using subplots to make an array of plots, but because some of the

plots have wider y-axis tick labels than others, some of the subplots end up

looking too close to each other. Here’s an image that shows what I mean:

http://web.mit.edu/wgrover/www/spacing.png

I’m currently using pylab.subplots_adjust(hspace = __, vspace = __) to

adjust the subplot spacing, but since that applies to all subplots, no one

setting looks right for the entire array of plots. Is there any way to set

the spacing so that the subplots plus tick labels are evenly distributed?

Or can I manually specify the spacing between each subplot? Thanks,

It is possible, but it is probably would be an incredible amount of work

(unless someone knows of some nifty trick that I am not aware of). My

suggestion to make things appear more “even” is to adjust the position of

the ylabels in the second column to that it takes up more room between the

first two columns.

The hard way would be to manually specify the extents in the “position”

kwarg [left, bottom, width, height] for the constructor of the axes object.

The values for those four parts would be in the coordinate system of the

figure object.

And this would not be super-hard. Opposed to the automatical

solution. I’m not familiar with pyplot etc., only with the API, but I

looked it up for you:

http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.axes

Notice that the tuple specifies the plot region, i.e. the

“decorations” like labels and tick labels are not included and will be

placed around.

You can also play with the Axes returned by the axes() call. I.e. you

can shift them around, using:

http://matplotlib.sourceforge.net/api/axes_api.html#matplotlib.axes.Axes.set_position.

Friedrich

2010/12/13 Will Grover <wgrover@...1166...>:

A belated thank you to Ben and Friedrich for your suggestions! I'm getting
good results shifting subplots around using Axes.set_position() as Friedrich
suggested, but I've noticed that whenever I shift a subplot into the region
where another subplot would be, the subplot I'm shifting just disappears.
This code demonstrates this; it creates a figure of six subplots and
attempts to shift subplots 5 and 6 to the right:
import pylab
offset = 0.04 # works for 0.04, but subplot 5 disappears at >0.05
pylab.subplot(2,3,1)
pylab.subplot(2,3,2)
pylab.subplot(2,3,3)
pylab.subplot(2,3,4)
pylab.subplot(2,3,5)
bbox=pylab.gca().get_position()
pylab.gca().set_position([bbox.x0 + offset, bbox.y0, bbox.x1-bbox.x0,
bbox.y1-bbox.y0])
pylab.subplot(2,3,6)
bbox=pylab.gca().get_position()
pylab.gca().set_position([bbox.x0 + offset, bbox.y0, bbox.x1-bbox.x0,
bbox.y1-bbox.y0])
pylab.savefig("out.png")
Setting offset to 0.04 results in subplots 5 and 6 being shifted to the
right a bit, as expected (see 0_04_offset.png), but if offset >= 0.05
subplot 5 disappears (see 0_05_offset.png). The funny thing is that you can
keep increasing offset and subplot 6 keeps moving (see 0_20_offset.png), so
it seems that you can use Axes.set_position() to move subplots anywhere
*except* into the space formerly occupied by another subplot. Is this
correct? Is there any way around this or am I using the wrong tool for the
job? Thanks again for your help,

This looks pretty much like a bug to me, since insets should place
Axes inside of other Axes. So things should not behave that way. But
my knowledge of this part of the API is pretty low, so someone else
with better knowledge should chime in? If at least Ben agrees on that
this is a bug, please file a bug report (it's somewhere in the sf
interface I believe).

Maybe, just to make a sanity check, make an even more simple script
with only two Axes. And afaik it's possible to attach files to bug
reports, so attach that script please. And: It really appears only if
shifting the bbox of an existent Axes? It works if a new Axes is
created with the offset from the beginning (I strongly believe it
does)? Your calls look ok to me.

I'm quite short of free time currently. Poke me in about a month if
you can efford this.

I'm not sure this helps (maybe in future it'll help :slight_smile:
Friedrich

(to those more knowledgeable than me...)

Is this something that ImageGrid would solve?

http://matplotlib.sourceforge.net/mpl_toolkits/axes_grid/users/overview.html#axes-grid1

Also, could you use subplotpar to adjust spacing in each subplot?

http://matplotlib.sourceforge.net/users/gridspec.html#adjust-gridspec-layout

Thanks,

Jason

···

On 10/28/10 1:18 PM, Will Grover wrote:

Hello matplotlib-users,

I'm using subplots to make an array of plots, but because some of the plots
have wider y-axis tick labels than others, some of the subplots end up
looking too close to each other. Here's an image that shows what I mean:

   http://web.mit.edu/wgrover/www/spacing.png

I'm currently using pylab.subplots_adjust(hspace = __, vspace = __) to
adjust the subplot spacing, but since that applies to all subplots, no one
setting looks right for the entire array of plots. Is there any way to set
the spacing so that the subplots *plus tick labels* are evenly distributed?
  Or can I manually specify the spacing between each subplot? Thanks,