Matplotlib coord. sys origin to top left


Question

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.

1
15
7/21/2014 12:59:20 PM

Accepted Answer

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()
9
8/29/2009 1:31:45 AM

The easiest way is to use:

plt.gca().invert_yaxis()

After you plotted the image. Origin works only for imshow.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon