subplots_adjust -- what am I doing wrong?

Hello, I am trying to plot a 2x3 matrix of (sub)images, with no spacing in between.

I thought the desired result could be achieved by the following code, but there is
still blank space (in the vertical-direction) between the subplots:

···

#--------------
import pylab
from scipy import randn

data=randn(575)
data.shape=(23,25)

fig1 = pylab.figure()

for i in range(6):
fig1.add_subplot(2,3,i+1)
pylab.imshow(data)

fig1.subplots_adjust(wspace=0,hspace=0.0)

pylab.show()
#--------------

I guess I am asking in general the proper way to display an NxM matrix of images with no spaces in between,
as the above code is not working.

Thank you for the help!
-adam.

By default, matplotlib will resize your axes to preserve the aspect
ratio of your image, which in the case of your 23x25 images is not
equal. You can override this behavior by setting aspect='auto'

for i in range(6):
  fig1.add_subplot(2,3,i+1)
  pylab.imshow(data, aspect='auto')

JDH

···

On Tue, Sep 9, 2008 at 6:26 AM, Adam <adam.listmail@...287...> wrote:

I thought the desired result could be achieved by the following code, but
there is
still blank space (in the vertical-direction) between the subplots:

John,

By default, matplotlib will resize your axes to preserve the aspect

ratio of your image, which in the case of your 23x25 images is not

equal. You can override this behavior by setting aspect=‘auto’

for i in range(6):

fig1.add_subplot(2,3,i+1)

pylab.imshow(data, aspect=‘auto’)

This gets rid of the blank space, but at the expense of the aspect ratio, as
you mention. Isn’t there away to preserve the aspect ratios of the individual

sub-images, and just remove the white space between them? I really to not
want to mess with the images themselves.

cheers,
adam.

Yes, but you will need to use the "axes" command rather than the
"subplot" command, to position the axes where you want them. This
will take a little arithmetic, since the axes parameters are fractions
of the figure parameters. So you will want to create a figsize and
axes that in combination have the right aspect ratio, and issue the
axes commands such that there is no space between them.

JDH

···

On Tue, Sep 9, 2008 at 7:12 AM, Adam <adam.listmail@...287...> wrote:

This gets rid of the blank space, but at the expense of the aspect ratio, as
you mention. Isn't there away to preserve the aspect ratios of the
individual
sub-images, and just remove the white space between them? I really to not
want to mess with the images themselves.

Adam,

I needed a same thing a while ago and I ended up with a custom class.
Although this need some more work, try and see if it fits your need in
case you want to avoid the little arithmetic that John mentioned.
The python file and a example plot are attached.

I wanted the padding between the subaxes is conserved even if the
figure size changes (and something more), so the implementation got
little bit hackish and much longer than I initially expected, but it
works for me.
A simple use case is included in the file (I'm afraid that there are
not much documentation).

Briefly,

import matplotlib.pyplot as plt
from mpl_multiaxes import MultiPane_Subplot

F = plt.figure(1)
F.clf()

mp = MultiPane_Subplot(F, subplot_pos=(1, 1, 1),
                       nrows_ncols = (3, 2),
                       n_pane=5,
                       pane_direction="row", # or "column"
                       axes_pad_inch=0.0,
                       )

F.add_subplot(mp)

In the above example, MultiPane_Subplot is a subclass of the Subplot
which contains subaxes. You set number of rows and columns for your
subaxes grid. If the number of subaxes is smaller than nrows*ncols,
you explicitly set it with n_pane.

After this, each subaxes is accessed as mp[0], mp[1], and so on. They
are subclass of Axes. So you may use any drawing method that Axes
class has. As mp is subclass of Subplot, it is also an axes. I
consider it as a kind of master axes. Anything you plot in this axes
is also drawn in all of its subaxes. Also note that xaxis and yaxis
are shared among these subaxes, therefore they have same xlim, ylim.
etc.

Regards,

-JJ

mpl_multiaxes.py (13.4 KB)

···

On Tue, Sep 9, 2008 at 8:12 AM, Adam <adam.listmail@...287...> wrote:

John,

By default, matplotlib will resize your axes to preserve the aspect
ratio of your image, which in the case of your 23x25 images is not
equal. You can override this behavior by setting aspect='auto'

for i in range(6):
fig1.add_subplot(2,3,i+1)
pylab.imshow(data, aspect='auto')

This gets rid of the blank space, but at the expense of the aspect ratio, as
you mention. Isn't there away to preserve the aspect ratios of the
individual
sub-images, and just remove the white space between them? I really to not
want to mess with the images themselves.

cheers,
adam.

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great
prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options

JJ,

Thank you for sharing this. Although I have to admit I’m a bit surprised by what
it takes to accomplish this, I think I will be able to use your class to do what I need
to do, or at least get a lot of the way there.

Cheers,
-adam.

···

On Tue, Sep 9, 2008 at 5:34 PM, Jae-Joon Lee <lee.j.joon@…287…> wrote:

Adam,

I needed a same thing a while ago and I ended up with a custom class.

Although this need some more work, try and see if it fits your need in

case you want to avoid the little arithmetic that John mentioned.

The python file and a example plot are attached.

I wanted the padding between the subaxes is conserved even if the

figure size changes (and something more), so the implementation got

little bit hackish and much longer than I initially expected, but it

works for me.

A simple use case is included in the file (I’m afraid that there are

not much documentation).

Briefly,

import matplotlib.pyplot as plt

from mpl_multiaxes import MultiPane_Subplot

F = plt.figure(1)

F.clf()

mp = MultiPane_Subplot(F, subplot_pos=(1, 1, 1),

                   nrows_ncols = (3, 2),

                   n_pane=5,

                   pane_direction="row", # or "column"

                   axes_pad_inch=0.0,

                   )

F.add_subplot(mp)

In the above example, MultiPane_Subplot is a subclass of the Subplot

which contains subaxes. You set number of rows and columns for your

subaxes grid. If the number of subaxes is smaller than nrows*ncols,

you explicitly set it with n_pane.

After this, each subaxes is accessed as mp[0], mp[1], and so on. They

are subclass of Axes. So you may use any drawing method that Axes

class has. As mp is subclass of Subplot, it is also an axes. I

consider it as a kind of master axes. Anything you plot in this axes

is also drawn in all of its subaxes. Also note that xaxis and yaxis

are shared among these subaxes, therefore they have same xlim, ylim.

etc.

Regards,

-JJ

On Tue, Sep 9, 2008 at 8:12 AM, Adam <adam.listmail@…287…> wrote:

John,

By default, matplotlib will resize your axes to preserve the aspect

ratio of your image, which in the case of your 23x25 images is not

equal. You can override this behavior by setting aspect=‘auto’

for i in range(6):

fig1.add_subplot(2,3,i+1)

pylab.imshow(data, aspect=‘auto’)

This gets rid of the blank space, but at the expense of the aspect ratio, as

you mention. Isn’t there away to preserve the aspect ratios of the

individual

sub-images, and just remove the white space between them? I really to not

want to mess with the images themselves.

cheers,

adam.


This SF.Net email is sponsored by the Moblin Your Move Developer’s challenge

Build the coolest Linux based applications with Moblin SDK & win great

prizes

Grand prize is a trip for two to an Open Source event anywhere in the world

http://moblin-contest.org/redirect.php?banner_id=100&url=/


Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users