Sometimes you want to flip the direction of the x-axis or y-axis in a chart. This helps show data in a way that is easier to understand or matches how you think about it.
0
0
Inverted axes in Matplotlib
Introduction
When you want the x-axis to go from right to left instead of left to right.
When you want the y-axis to go from top to bottom instead of bottom to top.
When comparing data that naturally decreases or reverses direction.
When you want to highlight a different perspective in your plot.
When plotting time series backward or showing ranks from highest to lowest.
Syntax
Matplotlib
ax.invert_xaxis() ax.invert_yaxis()
Use invert_xaxis() to flip the x-axis direction.
Use invert_yaxis() to flip the y-axis direction.
Examples
This flips the x-axis so it goes from 3 to 1 instead of 1 to 3.
Matplotlib
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.plot([1, 2, 3], [4, 5, 6]) ax.invert_xaxis() plt.show()
This flips the y-axis so it goes from 6 down to 4 instead of 4 up to 6.
Matplotlib
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.plot([1, 2, 3], [4, 5, 6]) ax.invert_yaxis() plt.show()
This flips both axes, reversing the directions of x and y.
Matplotlib
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.plot([1, 2, 3], [4, 5, 6]) ax.invert_xaxis() ax.invert_yaxis() plt.show()
Sample Program
This program plots points and then flips both the x-axis and y-axis directions. The plot will show x values decreasing from left to right and y values decreasing from bottom to top.
Matplotlib
import matplotlib.pyplot as plt # Create some data x = [10, 20, 30, 40] y = [100, 200, 300, 400] fig, ax = plt.subplots() ax.plot(x, y, marker='o') # Invert the x-axis and y-axis ax.invert_xaxis() ax.invert_yaxis() ax.set_title('Plot with Inverted Axes') ax.set_xlabel('X values (inverted)') ax.set_ylabel('Y values (inverted)') plt.show()
OutputSuccess
Important Notes
Inverting axes does not change the data, only how it is shown.
You can invert axes anytime after creating the plot but before showing it.
Inverted axes are useful for special cases like ranking or reverse time.
Summary
Use invert_xaxis() and invert_yaxis() to flip axis directions.
Inverted axes help show data from a different perspective.
They are easy to apply and do not change the original data.