exact figure size frustration

Hi, list-

This question has been asked before, but all of my search results don't address my problem:
<code>
fig = figure(1, figsize=(3.25, 3))
plot([0,1,5,2,9])
title('title')
xlabel('xAxis')
ylabel('yAxis')
fig.savefig('test.png',dpi=600)
</code>
The resulting figure is 2040x1890 pixels, or 3.4"x3.15", and the xlabel is cut off. Looking at the PNG file in an image editor, it appears that the axes and ticklabels fit the desired size. I've tried taking the difference from the output size and requested size and feeding that back in (3.25 - (3.4-3.25) = 3.10, but matplotlib seems to add an arbitrary buffer and it still doesn't come out to the desired size. How does one make an overall figure that is the desired size?

Thanks,
Ethan

Hi, list-

This question has been asked before, but all of my search results don't
address my problem:
<code>
fig = figure(1, figsize=(3.25, 3))
plot([0,1,5,2,9])
title('title')
xlabel('xAxis')
ylabel('yAxis')
fig.savefig('test.png',dpi=600)
</code>
The resulting figure is 2040x1890 pixels, or 3.4"x3.15", and the xlabel
is cut off. Looking at the PNG file in an image editor, it appears that
the axes and ticklabels fit the desired size. I've tried taking the
difference from the output size and requested size and feeding that back
in (3.25 - (3.4-3.25) = 3.10, but matplotlib seems to add an arbitrary
buffer and it still doesn't come out to the desired size. How does one
make an overall figure that is the desired size?

Ethan,

There seem to be two questions here. First, when I run your code, I get a png file of the right size:
  test.png: PNG image data, 1950 x 1800, 8-bit/color RGBA, non-interlaced

What version of mpl are you using? I don't recall that it ever had the property you are reporting, generating a larger figure than requested.

The second question is about the xlabel getting cut off. This is happening because mpl is using default subplot parameters that leave plenty of space for tick labels and axis labels with the default figure size, but don't leave enough if the figure is much smaller, and leave too much if the figure is much bigger. The subplot parameters are expressed as fractions of the figure size, but the text does not scale automatically with the figure size. Therefore you have to either specify the Axes position manually to leave the right size margins, or use subplots_adjust. When a figure is displayed on the screen, there is a button on the toolbar that brings up a subplots_adjust widget; this can be used to find values appropriate for your figure size, which you can then supply to your script. In the standard set of mpl examples there are many instances of subplots_adjust, e.g.,
http://matplotlib.sourceforge.net/examples/pylab_examples/subplots_adjust.html

Eric

···

On 01/07/2012 07:01 PM, Ethan Swint wrote:

Thanks,
Ethan

------------------------------------------------------------------------------
Ridiculously easy VDI. With Citrix VDI-in-a-Box, you don't need a complex
infrastructure or vast IT resources to deliver seamless, secure access to
virtual desktops. With this all-in-one solution, easily deploy virtual
desktops for less than the cost of PCs and save 60% on VDI infrastructure
costs. Try it free! http://p.sf.net/sfu/Citrix-VDIinabox
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Eric-

Thanks for your reply. I actually had the MPL version copied, but somehow I forgot to paste it: matplotlib.__version__ is 1.0.1 on Python 2.7.2, Ipython v. 0.11, on Windoze 7 x64. Around 2AM I found that if I called fig.set_size_inches(3.25,3) just before calling fig.savefig(), it did produce the proper size image.
For your viewing pleasure, on my system I get:
<code>
In [228]: fig = figure(3, figsize=(3.15, 3.15))
In [229]: fig.savefig('test0.png',dpi=600)
In [230]: fig.get_size_inches()
Out[230]: array([ 3.3, 3.3])
<\code>
The output is indeed 3.3"x3.3" (1980x1980). I'll upgrade to MPL 1.1.0 and see if that helps the initial size problem.
As for subplots_adjust - I'll have to dig into that. Will I need to make my figure with subplot(111) instead of figure()? Hmm - off to experiment.

Thanks,
Ethan

···

On 1/8/2012 2:29 AM, Eric Firing wrote:

On 01/07/2012 07:01 PM, Ethan Swint wrote:

Hi, list-

This question has been asked before, but all of my search results don't
address my problem:
<code>
fig = figure(1, figsize=(3.25, 3))
plot([0,1,5,2,9])
title('title')
xlabel('xAxis')
ylabel('yAxis')
fig.savefig('test.png',dpi=600)
</code>
The resulting figure is 2040x1890 pixels, or 3.4"x3.15", and the xlabel
is cut off. Looking at the PNG file in an image editor, it appears that
the axes and ticklabels fit the desired size. I've tried taking the
difference from the output size and requested size and feeding that back
in (3.25 - (3.4-3.25) = 3.10, but matplotlib seems to add an arbitrary
buffer and it still doesn't come out to the desired size. How does one
make an overall figure that is the desired size?

Ethan,

There seem to be two questions here. First, when I run your code, I get
a png file of the right size:
   test.png: PNG image data, 1950 x 1800, 8-bit/color RGBA, non-interlaced

What version of mpl are you using? I don't recall that it ever had the
property you are reporting, generating a larger figure than requested.

The second question is about the xlabel getting cut off. This is
happening because mpl is using default subplot parameters that leave
plenty of space for tick labels and axis labels with the default figure
size, but don't leave enough if the figure is much smaller, and leave
too much if the figure is much bigger. The subplot parameters are
expressed as fractions of the figure size, but the text does not scale
automatically with the figure size. Therefore you have to either
specify the Axes position manually to leave the right size margins, or
use subplots_adjust. When a figure is displayed on the screen, there is
a button on the toolbar that brings up a subplots_adjust widget; this
can be used to find values appropriate for your figure size, which you
can then supply to your script. In the standard set of mpl examples
there are many instances of subplots_adjust, e.g.,
http://matplotlib.sourceforge.net/examples/pylab_examples/subplots_adjust.html