Matplotlib coord. sys origin to top left
Asked Answered
S

5

26

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.

Seamark answered 28/8, 2009 at 20:40 Comment(0)
A
10

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()
Adult answered 29/8, 2009 at 1:31 Comment(1)
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!Seamark
L
35

The easiest way is to use:

plt.gca().invert_yaxis()

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

Lusitania answered 10/3, 2016 at 13:21 Comment(2)
This worked great when putting maps on satellite imagery with Cartopy. Note that, at least in my use case, this has to run AFTER the imshow().Thanks!Tremble
In case you're using subplots and sharex/sharey, then call this function only once otherwise for even number of cols/rows, you'll end up in the unchanged way again.Ulcerative
K
13

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.

Kimble answered 29/8, 2009 at 1:27 Comment(2)
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.Seamark
"axis ij just makes the y-axis increase downward instead of upward, right?" That is/was not always true: "axis ij places the coordinate system origin in the upper-left corner. The i-axis is vertical, with values increasing from top to bottom. The j-axis is horizontal with values increasing from left to right." (ece.northwestern.edu/local-apps/matlabhelp/techdoc/ref/…)Associative
A
10

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()
Adult answered 29/8, 2009 at 1:31 Comment(1)
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!Seamark
V
8

The following is a basic way to achieve this

ax=pylab.gca()

ax.set_ylim(ax.get_ylim()[::-1])
Vollmer answered 25/7, 2012 at 15:22 Comment(0)
C
6

This

plt.ylim(max(plt.ylim()), min(plt.ylim()))

has an advantage over this

plt.gca().invert_yaxis()

and is that if you are in interactive mode and you repeatedly plot the same plot (maybe with updated data and having a breakpoint after the plot) the y axis won't keep inverting every time.

Culhert answered 7/10, 2016 at 17:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.