How to invert the x or y axis
Asked Answered
Q

11

427

I have a scatter plot graph with a bunch of random x, y coordinates. Currently the Y-Axis starts at 0 and goes up to the max value. I would like the Y-Axis to start at the max value and go up to 0.

points = [(10,5), (5,11), (24,13), (7,8)]    
x_arr = []
y_arr = []
for x,y in points:
    x_arr.append(x)
    y_arr.append(y)
plt.scatter(x_arr,y_arr)
Quartermaster answered 12/1, 2010 at 19:31 Comment(3)
the amount of correct answers to this questions just shows how confusing this library is in useNaturism
@Naturism Since at least 10 years there is one obvious way and that is calling the invert_yaxis() method.Diaper
@Diaper what is obvious to you might not be obvious to another :)Naturism
W
817

There is a new API that makes this even simpler.

plt.gca().invert_xaxis()

and/or

plt.gca().invert_yaxis()
Wileywilfong answered 26/11, 2011 at 18:12 Comment(4)
Be aware that you have to set the axis limits before you invert the axis, otherwise it will un-invert it again.Bathroom
Would sure be nice if it took a Boolean argument. When called repeatedly, you're just flipping it back and forth.Cleora
@Tim Whitcomb's answer below works for specific axis objects. Is there a way to apply invert_yaxis() to a particular axis? (Distinction can be important when using sublots.)Cristoforo
Anyone who agrees with @Mastiff, please see my answer which uses the newer set_inverted method.Similitude
Q
92

DisplacedAussie's answer is correct, but usually a shorter method is just to reverse the single axis in question:

plt.scatter(x_arr, y_arr)
ax = plt.gca()
ax.set_ylim(ax.get_ylim()[::-1])

where the gca() function returns the current Axes instance and the [::-1] reverses the list.

Quiche answered 12/1, 2010 at 22:4 Comment(0)
T
59

You could also use function exposed by the axes object of the scatter plot

scatter = plt.scatter(x, y)
ax = scatter.axes
ax.invert_xaxis()
ax.invert_yaxis()
Tashinatashkent answered 3/7, 2019 at 20:30 Comment(1)
I was wondering why my 3D wireframe was not giving me the same date as the map. Invert the x axis.Waadt
P
24

Use matplotlib.pyplot.axis()

axis([xmin, xmax, ymin, ymax])

So you could add something like this at the end:

plt.axis([min(x_arr), max(x_arr), max(y_arr), 0])

Although you might want padding at each end so that the extreme points don't sit on the border.

Portmanteau answered 12/1, 2010 at 20:42 Comment(1)
This has an advantage over the first two answers that it fixes the orientation, not flip it every time (which is an issue if you need to call it in a loop).Underexposure
F
23

If you're in ipython in pylab mode, then

plt.gca().invert_yaxis()
show()

the show() is required to make it update the current figure.

Finial answered 11/10, 2012 at 14:34 Comment(0)
K
15

Another similar method to those described above is to use plt.ylim for example:

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

This method works for me when I'm attempting to compound multiple datasets on Y1 and/or Y2

Knisley answered 27/5, 2015 at 7:26 Comment(1)
This is the simplest method and should be on the top. Here is an x-axis version for anyone looking to directly copy - plt.xlim(max(x_axis_array), min(x_axis_array))Bickering
O
8

using ylim() might be the best approach for your purpose:

xValues = list(range(10))
quads = [x** 2 for x in xValues]
plt.ylim(max(quads), 0)
plt.plot(xValues, quads)

will result:enter image description here

Orpine answered 24/3, 2017 at 12:30 Comment(1)
How is this different to @Mortsde's answer?Durst
P
3

Alternatively, you can use the matplotlib.pyplot.axis() function, which allows you inverting any of the plot axis

ax = matplotlib.pyplot.axis()
matplotlib.pyplot.axis((ax[0],ax[1],ax[3],ax[2]))

Or if you prefer to only reverse the X-axis, then

matplotlib.pyplot.axis((ax[1],ax[0],ax[2],ax[3]))

Indeed, you can invert both axis:

matplotlib.pyplot.axis((ax[1],ax[0],ax[3],ax[2]))
Prevot answered 10/9, 2015 at 12:53 Comment(0)
S
2

if you are doing this in a subplot, here is the solution i found

fig, ax = plt.subplots(1,2, sharex = True)
for i in range(2):
    ax[i].plot(xdata, ydata)
axs = plt.gca()
axs.invert_xaxis()

While using sharex, last two lines needs to be outside the for loop

Sarchet answered 10/5, 2023 at 22:54 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Assyria
D
0

If using matplotlib you can try: matplotlib.pyplot.xlim(l, r) matplotlib.pyplot.ylim(b, t)

These two lines set the limits of the x and y axes respectively. For the x axis, the first argument l sets the left most value, and the second argument r sets the right most value. For the y axis, the first argument b sets the bottom most value, and the second argument t sets the top most value.

Dinkins answered 6/9, 2019 at 9:16 Comment(0)
S
0

Since Matplotlib v3.1 there is also the set_inverted method on an axis. The advantage of this over the invert_[xy]axis used in the accepted answer is that you get the same result regardless of how many times you call it. I.e. invert_[xy]axis reverses the current direction whereas set_inverted(False) makes it increasing and set_inverted(True) makes it decreasing.

ax = plt.gca()
ax.xaxis.set_inverted(True)
ax.yaxis.set_inverted(True)
Similitude answered 9/4 at 13:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.