# strange axis limits behavior after using aspect_ratio

Hello -

As reported in an earlier post, when setting aspect ratio, the axis limits don’t get updated correctly it seems. Or maybe I have to make another function call. Very easy example:

from pylab import *
ax = subplot(211)
plot([1,2,3])
print ax.get_xlim() # Gives you (0.0, 2.0), which is incorrect, as the data limits have been stretched.
draw()
print ax.get_xlim() # Gives (-1.8243394308943093, 3.8243394308943093) or something like it, which is correct

I don’t want to call draw, so is there some other function I can call to update the axis limits? Should that function be called automatically from set_aspect ?

I am using mpl 0.92.1. Thanks, Mark

You can call

ax.apply_aspect()

to do the aspect ratio calculations -- seems to work for me here with your example.

The aspect ratio code has always felt like a bit of a black art to me (it's a seemingly "necessarily complex" piece of code). Maybe someone else can answer -- is there a reason not to call apply_aspect() from set_aspect() besides a little extra computation? It obviously will still have to be called from draw (in case the figure size changes), but does it hurt to do it one extra time?

Cheers,
Mike

Mark Bakker wrote:

···

Hello -

As reported in an earlier post, when setting aspect ratio, the axis limits don't get updated correctly it seems. Or maybe I have to make another function call. Very easy example:

from pylab import *
ax = subplot(211)
plot([1,2,3])
print ax.get_xlim() # Gives you (0.0, 2.0), which is incorrect, as the data limits have been stretched.
draw()
print ax.get_xlim() # Gives (-1.8243394308943093, 3.8243394308943093) or something like it, which is correct

I don't want to call draw, so is there some other function I can call to update the axis limits? Should that function be called automatically from set_aspect ?

I am using mpl 0.92.1. Thanks, Mark

--
Michael Droettboom
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
Operated by AURA for NASA

Interesting, I didn’t even know about apply_aspect. And it does indeed work for my case.
Any reason not to call that at the end of the set_aspect function? Eric Firing probably knows.

I think the aspect ratio functionality is very cool.
You can set all kind of preferences, including an arbitrary aspect ratio.
I don’t know any other plotting packages that does it so well.
Whether the code is complex or not I don’t know.
But there is a lot of crap to have to keep track off.

Mark

···

On Thu, Feb 14, 2008 at 2:25 PM, Michael Droettboom <mdroe@…86…> wrote:

You can call

ax.apply_aspect()

to do the aspect ratio calculations – seems to work for me here with

The aspect ratio code has always felt like a bit of a black art to me

(it’s a seemingly “necessarily complex” piece of code). Maybe someone

else can answer – is there a reason not to call apply_aspect() from

set_aspect() besides a little extra computation? It obviously will

still have to be called from draw (in case the figure size changes), but

does it hurt to do it one extra time?

Cheers,

Mike

Mark Bakker wrote:

Hello -

As reported in an earlier post, when setting aspect ratio, the axis

limits don’t get updated correctly it seems. Or maybe I have to make

another function call. Very easy example:

from pylab import *

ax = subplot(211)

plot([1,2,3])

print ax.get_xlim() # Gives you (0.0, 2.0), which is incorrect, as the

data limits have been stretched.

draw()

print ax.get_xlim() # Gives (-1.8243394308943093, 3.8243394308943093) or

something like it, which is correct

I don’t want to call draw, so is there some other function I can call to

update the axis limits? Should that function be called automatically

from set_aspect ?

I am using mpl 0.92.1. Thanks, Mark

Michael Droettboom

Science Software Branch

Operations and Engineering Division

Space Telescope Science Institute

Operated by AURA for NASA

Mark Bakker wrote:

Hello -

As reported in an earlier post, when setting aspect ratio, the axis limits don't get updated correctly it seems. Or maybe I have to make another function call. Very easy example:

from pylab import *
ax = subplot(211)
plot([1,2,3])
print ax.get_xlim() # Gives you (0.0, 2.0), which is incorrect, as the data limits have been stretched.
draw()
print ax.get_xlim() # Gives (-1.8243394308943093, 3.8243394308943093) or something like it, which is correct

I don't want to call draw, so is there some other function I can call to update the axis limits? Should that function be called automatically from set_aspect ?

I am using mpl 0.92.1. Thanks, Mark

Mark,

In the present design, there is quite a bit that happens only when draw() is invoked; in particular, the apply_aspect() method is called when an axes is drawn. set_aspect merely sets the parameters that apply_aspect will use. It would not make sense for set_aspect to call apply_aspect.

I have not thought this through, but in place of the call to draw() you could try making the same call that the Axes.draw() method does:

ax.apply_aspect(ax.get_position(True))

Eric

Michael Droettboom wrote:

You can call

ax.apply_aspect()

to do the aspect ratio calculations -- seems to work for me here with your example.

The aspect ratio code has always felt like a bit of a black art to me (it's a seemingly "necessarily complex" piece of code). Maybe someone else can answer -- is there a reason not to call apply_aspect() from set_aspect() besides a little extra computation? It obviously will still have to be called from draw (in case the figure size changes), but does it hurt to do it one extra time?

Mike, Mark,

I answered this a minute ago before seeing the present set of messages, and without working through it carefully.

Now I see that, indeed, the simple default version of the call (no arguments) is identical to the version in the draw method.

It is possible that it would not actually hurt to call it in set_aspect, but I would need to look at that quite carefully, which I can't do right now. Maybe this evening or this weekend at the latest. And, the answer may be different for svn versus the present release; I will consider only svn.

I agree entirely that the aspect ratio code is complex, and painful to work with. It took a long time to get it to its present state--make it do most things reasonably; a case is found where it doesn't work; fix that; another problem pops up; fix that; and on and on--but throughout there has been a sense that surely there must be a better way!

The torture test for the aspect ratio code is making a plot (or worse, a set of subplots with shared axes) and then using the toolbar box-select and the pan/zoom control and the display window corner to manipulate it every which way. Then call set_aspect with a different setting and make sure it redraws sensibly, and do it all over again.

Eric

···

Cheers,
Mike

Mark Bakker wrote:

Hello -

As reported in an earlier post, when setting aspect ratio, the axis limits don't get updated correctly it seems. Or maybe I have to make another function call. Very easy example:

from pylab import *
ax = subplot(211)
plot([1,2,3])