Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse Y-Axis in PyPlot

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) 
like image 989
DarkAnt Avatar asked Jan 12 '10 19:01

DarkAnt


People also ask

How do you reverse an axis in Pyplot?

Most common method is by using invert_xaxis() and invert_yaxis() for the axes objects. Other than that we can also use xlim() and ylim(), and axis() methods for the pyplot object. To invert X-axis and Y-axis, we can use invert_xaxis() and invert_yaxis() function.

How do you reverse a histogram in Python?

To get a reverse-order cumulative histogram in Matplotlib, we can use cumulative = -1 in the hist() method. Set the figure size and adjust the padding between and around the subplots.

How do you rotate an axis in Python?

Rotate X-Axis Tick Labels in Matplotlib There are two ways to go about it - change it on the Figure-level using plt. xticks() or change it on an Axes-level by using tick. set_rotation() individually, or even by using ax.


2 Answers

There is a new API that makes this even simpler.

plt.gca().invert_xaxis() 

and/or

plt.gca().invert_yaxis() 
like image 129
Demitri Avatar answered Sep 18 '22 19:09

Demitri


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.

like image 34
Tim Whitcomb Avatar answered Sep 20 '22 19:09

Tim Whitcomb