 # playing with the aspect ratio

Hello, I am still trying to understand how to fix the

> aspect ration of figures generated with matplotlib. I
> made some progress but still have questions.

> In my script (appended to this mail) I do the following:

> 1) First I set up a figure as

f=figure(figsize=(3,2),frameon=False)
ax=axes()

3 >>> print ax.get_position()
[0.125, 0.10999999999999999, 0.77500000000000002, 0.79000000000000004]

The width and height are not equal in your axes. Perhaps you want

ax = axes([0.1, 0.1, 0.8, 0.8])

ax.update_datalim(((-0.5,-0.5),(2.5,1.5)))

I don't think calling update datalim manually is recommended. All you
need to is set the view limits, eg with the xlim, ylim or axis
commands. The datalim is there to assist the Axes class in
autoscaling. Since you know the view limits you want, just set the
viewlim yourself, after all the plot commands are issued. Eg

axis([-0.5,2.5,-0.5,1.5])

> My plan is that the lower left corner of the figure
> corresponds to (-0.5, -0.5) and the upper corner of the
> figure corresponds to (2.5,1.5) now. The x- and
> y-coordinate ranges have ratio 3:2 and the figure width
> and height also have ratio 3:2. Thus I expect the aspect
> ratio to be 1.

When you say lower-left of the figure, do you really mean the 0,0
coordinate in figure coordinates or the 0,0 coordinates in axes
coordinates? I think you mean the latter. If you want you axes to
consume the entire figure (no room for ticklabels), use

ax = axes([0,0,1,1])

> Questions: Is the combination of update_datalim and
> autoscale_view a good way to do this? Is there a more
> direct way to prescribe the coordinate range of the
> figure?

No, I don't think so, as above.

> 2) I try to draw some squares using commands like

> p = Rectangle((0.1, 0.1), 0.8, 0.8, fill=True,

> The resulting rectangle looks almost square, but
> inspecting the PostScript file reveals that it has width
> 44.64 and height 45.502 PostScript units. This seems to
> much to be caused by rounding errors.

> Question: why is the rectangle not square?

I think it's the problem mentioned above - your axes width and height
are not equal.

Does the code below help?

from matplotlib.matlab import *
from matplotlib.patches import Rectangle

f = figure(figsize=(3,2),frameon=False)

# ax is 0.8*3 inches wide, 0.8*2inches tall, ie 2.4 x 1.6
ax = axes([0.1, 0.1, 0.8, 0.8])
p = Rectangle((0.1, 0.1), 0.8, 0.8, fill=True, fc=(1,0,0))
axis([-0.5,2.5,-0.5,1.5])
savefig('test.ps')

> 3) The pattern of squares I draw is centered in the
> coordinate range. The resulting picture is not centered
> in the image window (and neither in the PostScript
> bounding box).

> Question: why is the picture not centred?

I'm a little confused here. If there is a problem with centering in
the bounding box, you might want to talk to the PS maintainer <wink>.
Perhaps with the suggestions I've made above incorporated, you can
post an example that replicates your problem.

Cheers,
JDH

Hello John,

3 >>> print ax.get_position()
[0.125, 0.10999999999999999, 0.77500000000000002, 0.79000000000000004]

Oh, ok! This explains why my picture is not centred.
0.125 + 0.775 + 0.1 = 1,
so the left margin is larger. I guess this is to make space for
labels, isn't it?

Does the code below help?

from matplotlib.matlab import *
from matplotlib.patches import Rectangle

f = figure(figsize=(3,2),frameon=False)

# ax is 0.8*3 inches wide, 0.8*2inches tall, ie 2.4 x 1.6
ax = axes([0.1, 0.1, 0.8, 0.8])
p = Rectangle((0.1, 0.1), 0.8, 0.8, fill=True, fc=(1,0,0))