views:

461

answers:

2

How can I flip the origin of a matplotlib plot to be in the upper-left corner - as opposed to the default lower-left? I'm using matplotlib.pylab.plot to produce the plot (though if there is another plotting routine that is more flexible, please let me know).

I'm looking for the equivalent of the matlab command: axis ij;

Also, I've spent a couple hours surfing matplotlib help and google but haven't come up with an answer. Some info on where I could have looked up the answer would be helpful as well.

Thanks!

+1  A: 

axis ij just makes the y-axis increase downward instead of upward, right? If so, then matplotlib.axes.invert_yaxis() might be all you need -- but I can't test that right now.

If that doesn't work, I found a mailing post suggesting that

setp(gca(), 'ylim', reversed(getp(gca(), 'ylim')))

might do what you want to resemble axis ij.

Mark Rushakoff
I never could get invert_yaxis() to work. If it did, that would be ideal. From what I could piece together, the setp command should work, but "reversed" returns an iterator. I couldn't get it to return a sequence instead, though I am still relatively new to this. So it may be easy.
Nate
A: 

For an image or contour plot, you can use the keyword origin = None | 'lower' | 'upper' and for a line plot, you can set the ylimits high to low.

from pylab import *
A = arange(25)/25.
A = A.reshape((5,5))

figure()
imshow(A, interpolation='nearest', origin='lower')

figure()
imshow(A, interpolation='nearest')

d = arange(5)
figure()
plot(d)
ylim(5, 0)

show()
tom10
Directly setting the axis limits with "ylim([y1,y2])" or "axis([x1,x2,y1,y2])" worked will. It's not completely generic, but I'm sure I can figure something out to make it generic. Maybe the setp command suggested above.Thanks for the help!
Nate