plot swaps axes specified with extent

Hi all,

I have a script that reads in mouse-click coordinates from an image.
I noticed that, with image extents specified, the axes flip whenever I
plot to them.

This snippet demonstrates the behaviour I see:

# -- START --

import pylab as P
import numpy as N

# Generate test pattern
x = N.arange(100).reshape(-1,1) + N.zeros(100)

def click(event):
    print "Mouse click at (%f,%f)" % (event.xdata,event.ydata)
    P.plot([event.xdata],[event.ydata],'o')
    P.draw()

P.imshow(x,extent=(0,x.shape[1],x.shape[0],0))
P.connect('button_press_event',click)
P.show()

# -- END --

Is this normal? If so, how do I get around the problem? I also
noticed that, even without extents, the image gets scaled after
plotting.

I'd appreciate any advice!

Thanks for your time.
Stéfan

Stefan,

That certainly looks to me like a bug, but it is not obvious to me after a quick look where the bug is (although I suspect it is very simple), and I can't look at it more right now. If someone else doesn't chime in with a fix, you might want to file a bug report on sourceforge to make sure it is not forgotten. Maybe I can take another look within the next few days.

What do you mean when you say "the image gets scaled after plotting"?

Eric

Stefan van der Walt wrote:

···

Hi all,

I have a script that reads in mouse-click coordinates from an image.
I noticed that, with image extents specified, the axes flip whenever I
plot to them.

This snippet demonstrates the behaviour I see:

# -- START --

import pylab as P
import numpy as N

# Generate test pattern
x = N.arange(100).reshape(-1,1) + N.zeros(100)

def click(event):
    print "Mouse click at (%f,%f)" % (event.xdata,event.ydata)
    P.plot([event.xdata],[event.ydata],'o')
    P.draw()

P.imshow(x,extent=(0,x.shape[1],x.shape[0],0))
P.connect('button_press_event',click)
P.show()

# -- END --

Is this normal? If so, how do I get around the problem? I also
noticed that, even without extents, the image gets scaled after
plotting.

I'd appreciate any advice!

Thanks for your time.
Stéfan

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Stefan,

Is this normal? If so, how do I get around the problem? I also
noticed that, even without extents, the image gets scaled after
plotting.

Try to set the "_autoscale" parameter of your current 'axes' to False. That
way, you should avoid any inopportune rescaling. For the image, try to use
aspect='auto'.

For example,

P.imshow(x,extent=(0,x.shape[1],x.shape[0],0))
P.gca().set_autoscale_on(False)
P.gca().set_aspect('auto')

Let us know if it works
P.

Hi Eric

That certainly looks to me like a bug, but it is not obvious to me after
a quick look where the bug is (although I suspect it is very simple),
and I can't look at it more right now. If someone else doesn't chime in
with a fix, you might want to file a bug report on sourceforge to make
sure it is not forgotten. Maybe I can take another look within the next
few days.

What do you mean when you say "the image gets scaled after
plotting"?

Thanks very much for having looked at this. My sentence above should
probably have read "the limits of the x-axis change after plotting".
They say two pictures are worth two-thousand words:

http://mentat.za.net/refer/before_plot.png
http://mentat.za.net/refer/after_plot.png

Regards
Stéfan

···

On Thu, Jul 27, 2006 at 02:57:31PM -1000, Eric Firing wrote:

Thanks, P., that did the trick! It looks like the right way to fix
the scaling of the axes extents, but I am still not sure whether the
axis flipping behaviour I described earlier is correct.

Regards
Stéfan

···

On Thu, Jul 27, 2006 at 08:57:47PM -0400, PGM wrote:

> Is this normal? If so, how do I get around the problem? I also
> noticed that, even without extents, the image gets scaled after
> plotting.

Try to set the "_autoscale" parameter of your current 'axes' to False. That
way, you should avoid any inopportune rescaling. For the image, try to use
aspect='auto'.

For example,

P.imshow(x,extent=(0,x.shape[1],x.shape[0],0))
P.gca().set_autoscale_on(False)

Stefan van der Walt wrote:

···

On Thu, Jul 27, 2006 at 08:57:47PM -0400, PGM wrote:

Is this normal? If so, how do I get around the problem? I also
noticed that, even without extents, the image gets scaled after
plotting.

Try to set the "_autoscale" parameter of your current 'axes' to False. That way, you should avoid any inopportune rescaling. For the image, try to use aspect='auto'.

For example,

P.imshow(x,extent=(0,x.shape[1],x.shape[0],0))
P.gca().set_autoscale_on(False)

Thanks, P., that did the trick! It looks like the right way to fix
the scaling of the axes extents, but I am still not sure whether the
axis flipping behaviour I described earlier is correct.

I changed it in svn 2636; now Axes.autoscale_view() preserves axis direction. I think this will be generally useful and will cause less user surprise than the previous behavior.

Eric