Skip to Content

Reverse Axes of a Plot in Matplotlib

The matplotlib module in Python is used to create highly customizable plots. The module comes with a lot of built-in 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 –

  1. Use the matplotlib axes object’s invert_axis() and invert_yaxis() functions to reverse the x-axis and the y-axis respectively. Use plt.gca() to get the current axes instance.
  2. Set the axis ranges in the opposite direction, that is, from the max value to the min value using the matplotlib.pyplot.xlim() and the matplotlib.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 - years
x = [2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020]
# y values - 1 USD in INR
y = [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 plot
plt.scatter(x, y)

# add axes labels
plt.xlabel('Year')
plt.ylabel('1USD in INR')

# add plot title
plt.title("Original Plot")

Output:

the resulting scatter plot

Example 1 – Using the invert_xaxis() and the invert_yaxis() functions

Let’s plot the above scatter plot but reverse its x-axis.

# plot x and y on scatter plot
plt.scatter(x, y)

# add axes labels
plt.xlabel('Year')
plt.ylabel('1USD in INR')

# reverse the x-axis
plt.gca().invert_xaxis()

# add plot title
plt.title("X-axis inverted")

Output:

scatter plot with x axis reversed

Here, we use plt.gca() to get the current axes instance and then apply the axes invert_xaxis() function to reverse the x-axis in the plot.

Let’s now invert the y-axis of the original scatter plot.

# plot x and y on scatter plot
plt.scatter(x, y)

# add axes labels
plt.xlabel('Year')
plt.ylabel('1USD in INR')

# reverse the y-axis
plt.gca().invert_yaxis()

# add plot title
plt.title("Y-axis inverted")

Output:

scatter plot with y-axis reversed

You can see that the y-axis is reversed in the above plot.

Note that you can reverse both the x-axis and the y-axis 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 x-axis.

# plot x and y on scatter plot
plt.scatter(x, y)

# add axes labels
plt.xlabel('Year')
plt.ylabel('1USD in INR')

# reverse the x-axis
plt.xlim((max(x), min(x)))

# add plot title
plt.title("X-axis inverted")

Output:

scatter plot with x axis reversed

You can see that the x-axis values are now reversed.

You can similarly reverse the y-axis by changing the y-axis range from max to min.

# plot x and y on scatter plot
plt.scatter(x, y)

# add axes labels
plt.xlabel('Year')
plt.ylabel('1USD in INR')

# reverse the y-axis
plt.ylim((max(y), min(y)))

# add plot title
plt.title("Y-axis inverted")

Output:

scatter plot with y axis reversed

The y-axis values are now reversed.

Again, you may reverse both axes depending on your use case.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.