361

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)
Mateusz Piotrowski
  • 6,866
  • 9
  • 49
  • 75
DarkAnt
  • 3,633
  • 2
  • 15
  • 6

10 Answers10

712

There is a new API that makes this even simpler.

plt.gca().invert_xaxis()

and/or

plt.gca().invert_yaxis()
Demitri
  • 11,446
  • 4
  • 34
  • 38
  • 69
    Be aware that you have to set the axis limits *before* you invert the axis, otherwise it will un-invert it again. – TheBigH Jan 22 '16 at 15:56
  • Would sure be nice if it took a Boolean argument. When called repeatedly, you're just flipping it back and forth. – Mastiff Feb 09 '21 at 18:13
77

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.

Community
  • 1
  • 1
Tim Whitcomb
  • 10,027
  • 3
  • 34
  • 47
38

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()
ShikharDua
  • 8,461
  • 1
  • 22
  • 21
  • I was wondering why my 3D wireframe was not giving me the same date as the map. Invert the x axis. – pierre Feb 23 '21 at 17:16
23

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.

DisplacedAussie
  • 4,438
  • 1
  • 25
  • 21
  • 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). – Bas Swinckels Aug 04 '19 at 14:45
22

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.

charles.fox
  • 463
  • 4
  • 10
9

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

Mortsde
  • 91
  • 1
  • 4
6

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

Farzad Vertigo
  • 2,083
  • 1
  • 22
  • 31
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]))
Luis DG
  • 448
  • 4
  • 8
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.

0

If using matplotlib we can make plot in reverse y axis in these 3 different ways: 1.By Specifying ymaximum value as y_min and yminimum as y_max

plt.xlim(x_min,x_max)
plt.ylim(y_min,y_max)
plt.xlim(0, 80)
plt.ylim(100, 0)
  1. Specifying axis

    plt.axis([x_min, x_max, y_min, y_max]) plt.axis([0, 80, 100, 0])

3.Using gca()function of matplotlib and calling invert_yaxis()function

ax = plt.gca()
ax.invert_yaxis()
DS_ShraShetty
  • 590
  • 3
  • 7