The matplotlib module in Python is used to create highly customizable plots. The module comes with a lot of builtin methods to help you customize your plots. In this tutorial, we will look at how to reverse the axes of a plot in matplotlib with the help of some examples.
Invert the axes in Maplotlib
You can reverse the axis of a matplotlib plot in the following ways –
 Use the matplotlib axes object’s
invert_axis()
andinvert_yaxis()
functions to reverse the xaxis and the yaxis respectively. Useplt.gca()
to get the current axes instance.  Set the axis ranges in the opposite direction, that is, from the max value to the min value using the
matplotlib.pyplot.xlim()
and thematplotlib.pyplot.ylim()
functions.
Let’s now look at some examples of using the above methods.
First, let’s create a scatter plot that we will be using throughout this tutorial.
import matplotlib.pyplot as plt# x values  yearsx = [2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020]# y values  1 USD in INRy = [46.67, 53.44, 56.57, 62.33, 62.97, 66.46, 67.79, 70.09, 70.39, 76.38]# plot x and y on scatter plotplt.scatter(x, y)# add axes labelsplt.xlabel('Year')plt.ylabel('1USD in INR')# add plot titleplt.title("Original Plot")
Output:
Example 1 – Using the invert_xaxis()
and the invert_yaxis()
functions
Let’s plot the above scatter plot but reverse its xaxis.
# plot x and y on scatter plotplt.scatter(x, y)# add axes labelsplt.xlabel('Year')plt.ylabel('1USD in INR')# reverse the xaxisplt.gca().invert_xaxis()# add plot titleplt.title("Xaxis inverted")
Output:
Here, we use plt.gca()
to get the current axes instance and then apply the axes invert_xaxis()
function to reverse the xaxis in the plot.
Introductory ⭐
 Harvard University Data Science: Learn R Basics for Data Science
 Standford University Data Science: Introduction to Machine Learning
 UC Davis Data Science: Learn SQL Basics for Data Science
 IBM Data Science: Professional Certificate in Data Science
 IBM Data Analysis: Professional Certificate in Data Analytics
 Google Data Analysis: Professional Certificate in Data Analytics
 IBM Data Science: Professional Certificate in Python Data Science
 IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
 Harvard University Learning Python for Data Science: Introduction to Data Science with Python
 Harvard University Computer Science Courses: Using Python for Research
 IBM Python Data Science: Visualizing Data with Python
 DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
 UC San Diego Data Science: Python for Data Science
 UC San Diego Data Science: Probability and Statistics in Data Science using Python
 Google Data Analysis: Professional Certificate in Advanced Data Analytics
 MIT Statistics and Data Science: Machine Learning with Python  from Linear Models to Deep Learning
 MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Let’s now invert the yaxis of the original scatter plot.
# plot x and y on scatter plotplt.scatter(x, y)# add axes labelsplt.xlabel('Year')plt.ylabel('1USD in INR')# reverse the yaxisplt.gca().invert_yaxis()# add plot titleplt.title("Yaxis inverted")
Output:
You can see that the yaxis is reversed in the above plot.
Note that you can reverse both the xaxis and the yaxis together.
Example 2 – Reverse axes by changing the range
In this method, we reverse the axes by specifying their range to go from max to min (instead of the usual min to max value) using the matplotlib.pyplot.xlim()
and the matplotlib.pyplot.ylim()
functions.
Let’s take the same use cases as above. First, let’s invert the xaxis.
# plot x and y on scatter plotplt.scatter(x, y)# add axes labelsplt.xlabel('Year')plt.ylabel('1USD in INR')# reverse the xaxisplt.xlim((max(x), min(x)))# add plot titleplt.title("Xaxis inverted")
Output:
You can see that the xaxis values are now reversed.
You can similarly reverse the yaxis by changing the yaxis range from max to min.
# plot x and y on scatter plotplt.scatter(x, y)# add axes labelsplt.xlabel('Year')plt.ylabel('1USD in INR')# reverse the yaxisplt.ylim((max(y), min(y)))# add plot titleplt.title("Yaxis inverted")
Output:
The yaxis values are now reversed.
Again, you may reverse both axes depending on your use case.
You might also be interested in –
 Add Trendline to a Maplotlib Plot with Code and Output
 How to Add Title to a Plot in Matplotlib? (Code Examples with Output)
 Set Axis Range (axis limits) in Matplotlib Plots
 Show Gridlines on Matplotlib Plots
 How to Label Points on a Scatter Plot in Matplotlib?
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.

Piyush Raj
Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.