I was working on a Python data visualization project where I had to display an image using Matplotlib’s imshow() function, but the image appeared upside down. It took me a moment to realize that the y-axis was inverted by default in imshow().
Since I’ve spent over a decade working with Python and Matplotlib, I’ve faced this issue many times before.
In this tutorial, I’ll show you how to invert the y-axis in Matplotlib’s imshow function using a few simple methods.
Matplotlib Invert the Y-Axis in imshow
When you use imshow() in Matplotlib, the origin of the image (0,0) is placed at the top-left corner by default. This means the y-axis increases from top to bottom, which is opposite to the usual Cartesian coordinate system.
In many Python projects, especially when visualizing geographical or scientific data, we prefer the y-axis to increase from bottom to top. That’s why learning how to invert or control the y-axis orientation is so important.
Method 1 – Invert the Y-Axis Using invert_yaxis()
The simplest way to invert the y-axis in Matplotlib is by using the built-in invert_yaxis() method. This method is part of the Axes object and can be applied after displaying the image with imshow().
Here’s how I usually do it in my Python projects.
Example: Invert the Y-Axis with invert_yaxis()
Before we jump into the code, make sure you have Matplotlib and NumPy installed.
You can install them using the following command:
pip install matplotlib numpyNow, let’s look at the full Python example.
import matplotlib.pyplot as plt
import numpy as np
# Create a sample dataset
data = np.arange(100).reshape(10, 10)
# Display the image using imshow
plt.imshow(data, cmap='viridis')
# Add a title
plt.title("Matplotlib imshow - Invert Y-Axis Example")
# Invert the y-axis
plt.gca().invert_yaxis()
# Display the plot
plt.show()You can see the output in the screenshot below.

This simple line, plt.gca().invert_yaxis(), flips the image vertically. Now, the y-axis increases from bottom to top, which matches the standard Cartesian coordinate layout.
I often use this approach when visualizing heatmaps or topographical data in Python, especially when the orientation matters for interpretation.
Method 2 – Control the Image Origin Using the origin Parameter
While the first method works perfectly, there’s an even cleaner way to handle this directly inside imshow(), by using the origin parameter.
The origin parameter accepts two main values:
- “upper” — places the [0,0] coordinate at the top-left corner (default)
- “lower” — places the [0,0] coordinate at the bottom-left corner
Here’s how you can use it.
Example: Use the origin Parameter in imshow()
import matplotlib.pyplot as plt
import numpy as np
# Generate a random dataset
data = np.random.randint(0, 255, (10, 10))
# Display the image with origin set to 'lower'
plt.imshow(data, cmap='plasma', origin='lower')
# Add labels and title
plt.title("Matplotlib imshow with origin='lower' (Python Example)")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
# Show the image
plt.show()You can see the output in the screenshot below.

By setting origin=’lower’, the image is displayed with the y-axis increasing upward.
This is my preferred method because it keeps the code clean and avoids extra function calls.
If you’re working with images or datasets that follow a geographic coordinate system (like latitude and longitude), this approach ensures that north points upward, just as it should.
Method 3 – Reverse the Y-Axis Using ylim()
Another way to invert the y-axis in Matplotlib is to manually reverse the y-axis limits using plt.ylim(). This method gives you more control, especially when you’re working with multiple subplots or custom axis limits.
Here’s how you can do it.
Example: Invert the Y-Axis with plt.ylim()
import matplotlib.pyplot as plt
import numpy as np
# Create some sample data for visualization
data = np.linspace(0, 1, 100).reshape(10, 10)
# Display the data using imshow
plt.imshow(data, cmap='coolwarm')
# Reverse the y-axis using ylim
plt.ylim(plt.ylim()[::-1])
# Add title and axis labels
plt.title("Invert Y-Axis in Matplotlib Using ylim()")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
# Show the plot
plt.show()You can see the output in the screenshot below.

The line plt.ylim(plt.ylim()[::-1]) reverses the order of the y-axis limits. This is a powerful trick when you want to invert axes dynamically without changing the original data or imshow parameters.
Method 4 – Invert the Y-Axis for Subplots in Matplotlib
If you’re using subplots in Python, you can apply the inversion on each subplot individually.
This is especially useful when comparing two images or datasets that need opposite orientations.
Here’s a quick example.
Example: Invert Y-Axis in Subplots
import matplotlib.pyplot as plt
import numpy as np
# Generate two random images
image1 = np.random.random((8, 8))
image2 = np.random.random((8, 8))
# Create subplots
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))
# Display the first image normally
ax1.imshow(image1, cmap='viridis')
ax1.set_title("Normal Y-Axis")
# Display the second image and invert its y-axis
ax2.imshow(image2, cmap='viridis')
ax2.invert_yaxis()
ax2.set_title("Inverted Y-Axis")
# Show the plots
plt.tight_layout()
plt.show()You can see the output in the screenshot below.

In this example, I used the invert_yaxis() method directly on the second subplot (ax2). This makes it easy to compare both orientations side by side in the same figure.
Bonus Tip – Combine imshow with Other Matplotlib Features
Once you’ve mastered controlling the y-axis, you can combine imshow() with other Matplotlib features like colorbars, annotations, and gridlines. This makes your visualizations more informative and professional.
Here’s a quick example showing how to add a colorbar after inverting the y-axis.
import matplotlib.pyplot as plt
import numpy as np
# Create a gradient dataset
data = np.linspace(0, 1, 100).reshape(10, 10)
# Display the image
plt.imshow(data, cmap='inferno', origin='lower')
# Add a colorbar
plt.colorbar(label='Intensity')
# Add title and labels
plt.title("Matplotlib imshow - Inverted Y-Axis with Colorbar")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
# Show the plot
plt.show()Adding a colorbar helps you interpret the data values visually, which is especially useful for heatmaps and scientific plots in Python.
Common Mistakes to Avoid
Here are a few common mistakes I’ve seen developers make when working with imshow() and inverted axes in Python:
- Forgetting to specify origin – This can confuse when your image appears flipped vertically.
- Mixing up axis inversion methods – Use either invert_yaxis() or origin=’lower’, but not both together.
- Not adjusting labels after inversion – Always double-check that your axis labels still make sense after flipping.
- Ignoring subplot independence – Each subplot has its own axis settings; inverting one doesn’t affect others.
By keeping these in mind, you’ll avoid a lot of confusion when working with image data in Matplotlib.
Practical Use Cases for Inverting the Y-Axis
In my 10+ years of Python experience, I’ve found this technique useful in several real-world scenarios:
- Image processing – When visualizing pixel data where the origin needs to be bottom-left.
- Geospatial mapping – When plotting latitude-longitude grids, where north should point upward.
- Heatmaps and sensor data – When aligning visualization orientation with physical layouts.
- Machine learning – When inspecting feature maps or convolutional layers visually.
So, whether you’re analyzing satellite images or visualizing machine learning outputs, knowing how to control the y-axis orientation can make your plots more intuitive.
When I first started using Matplotlib, I didn’t realize how much control I had over the axes.
But once I learned how to invert and adjust the y-axis properly, my visualizations became clearer and more professional.
If you’re working on a Python project that uses imshow(), I recommend experimenting with both invert_yaxis() and origin=’lower’. You’ll quickly see which approach fits your workflow best.
You may also like to read:
- Save Matplotlib Subplots to PDF in Python
- Save Multiple Pages to a PDF in Matplotlib
- Save Matplotlib Table as PDF in Python
- Flip Y-Axis Label in Matplotlib using Python

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.