As a developer, I’ve worked extensively with Matplotlib for data visualization. One common task I often encounter is the need to invert the y-axis in a plot. If you’ve ever wondered how to invert the y-axis in Matplotlib, you’re in the right place.
In this article, I’ll walk you through simple methods to achieve this, based on my firsthand experience. By the end, you’ll be able to confidently invert the y-axis in your plots and tailor your visualizations for better clarity.
Let’s start!
Methods to Invert the Y-Axis in Matplotlib
Now, I will explain to you the methods to invert the y-axis in Matplotlib.
Method 1: Use invert_yaxis() Function
The easiest and most direct way to invert the y-axis in Matplotlib is by using the invert_yaxis() method on the Axes object in Python.
Here’s a simple example:
import matplotlib.pyplot as plt
# Sample data: US city populations (in thousands)
cities = ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix']
populations = [8419, 3982, 2716, 2320, 1690]
plt.bar(cities, populations)
plt.gca().invert_yaxis() # Invert the y-axis
plt.title('City Populations with Inverted Y-Axis')
plt.ylabel('Population (in thousands)')
plt.show()I executed the above example code and added the screenshot below.

How this works:
plt.gca()gets the current Axes object.- Calling
.invert_yaxis()flips the y-axis so that the highest values appear at the bottom and the lowest at the top.
This method is quick and clean, especially when you are working with simple plots.
Check out Matplotlib Scatter Plot Legend
Method 2: Set ylim with Reversed Limits
Another way to invert the y-axis is by manually setting the y-axis limits in reverse order using plt.ylim() or ax.set_ylim() in Python.
Example:
import matplotlib.pyplot as plt
# Sample data: Average temperatures (°F) in US cities during winter
cities = ['Denver', 'Chicago', 'Boston', 'Seattle', 'Miami']
temps = [30, 25, 28, 40, 70]
plt.plot(cities, temps, marker='o')
plt.ylim(max(temps), min(temps)) # Set y-axis limits reversed
plt.title('Average Winter Temperatures with Inverted Y-Axis')
plt.ylabel('Temperature (°F)')
plt.show()I executed the above example code and added the screenshot below.

Here, I explicitly set the y-axis limits with the maximum value first and the minimum value second. This reverses the direction of the y-axis.
This approach gives you more control over the axis range, useful when you want to invert the axis but maintain specific bounds.
Read Matplotlib Multiple Bar Chart
Method 3: Invert Y-Axis in Subplots or Multiple Axes
When working with multiple plots or subplots, you might want to invert the y-axis on specific axes.
Here’s how you can do it:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
fig, axs = plt.subplots(2, 1, figsize=(8, 6))
# Normal plot
axs[0].plot(x, y)
axs[0].set_title('Normal Y-Axis')
# Inverted y-axis plot
axs[1].plot(x, y)
axs[1].invert_yaxis()
axs[1].set_title('Inverted Y-Axis')
plt.tight_layout()
plt.show()I executed the above example code and added the screenshot below.

This method is useful when comparing data side by side or when only one subplot requires an inverted y-axis.
Read How to Get Data From a GET Request in Django
When Not to Invert the Y-Axis
While inverting the y-axis can be helpful, it’s important to consider the audience’s expectations. For instance, in most line charts showing time series or stock prices, the y-axis increasing upwards is standard and intuitive.
Inverting the y-axis in these cases might confuse your viewers or misrepresent the data story. Always think about the context before flipping the axis.
Bonus Tip: Invert Both Axes
Sometimes, you might want to invert both x and y axes, for example, when dealing with image coordinates or certain scientific data.
You can do this by chaining the invert methods:
plt.gca().invert_xaxis()
plt.gca().invert_yaxis()Or by setting limits in reverse for both axes:
plt.xlim(max_x, min_x)
plt.ylim(max_y, min_y)Inverting the y-axis in Matplotlib is a simple yet useful way to customize your plots. Whether you use the invert_yaxis() method or reverse the axis limits manually, both approaches are valid and depend on your specific use case.
From my experience, invert_yaxis() is an easy and readable method, especially when working with simple plots. For more control over axis limits, setting ylim manually works great.
Other Matplotlib articles you may also like:
- Matplotlib x-axis Label
- Python Matplotlib tick_params
- Matplotlib tight_layout
- Best Fit a Line to a Scatter Plot in Python Matplotlib

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.