Open In App

How to Reverse Axes in Matplotlib

Last Updated : 16 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Matplotlib allows you to reverse X-axisY-axis or both using multiple approaches. This is useful when we want to visualize data from a different view like reversing the order of time in a time-series or switching the direction of the axes according to our needs.

Method 1: Using invert_xaxis() and invert_yaxis()

We can use invert_xaxis() to reverse X-axis and invert_yaxis() to reverse Y-axis. Here:

  • np.linspace(0, 10, 100): Generates 100 evenly spaced numbers between 0 and 10.
  • invert_xaxis(): Flips X-axis so values decrease from left to right.
  • invert_yaxis(): Flips Y-axis so values decrease from bottom to top.
  • subplots(): Creates multiple plots in one figure.
  • tight_layout(): Adjusts spacing to prevent overlap between plots.
Python
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 10, 100)
y1 = x**2
y2 = np.exp(x)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6))
ax1.plot(x, y1, label="x^2", color='b')
ax1.plot(x, y2, label="exp(x)", color='r')
ax1.set_title("Normal Plot")
ax1.set_xlabel("X-axis")
ax1.set_ylabel("Y-axis")
ax1.legend()
ax2.plot(x, y1, label="x^2", color='b')
ax2.plot(x, y2, label="exp(x)", color='r')
ax2.set_title("Inverted Plot")
ax2.set_xlabel("X-axis")
ax2.set_ylabel("Y-axis")
ax2.invert_xaxis()
ax2.invert_yaxis()
ax2.legend()
fig.tight_layout()
plt.show()

Output:

reverse1

Normal vs Inverted

Method 2: Using xlim() and ylim()

We can reverse X and Y axes by directly setting their limits using pyplot.xlim() and pyplot.ylim(). These functions allows to define the range of values for each axis. By setting limits in reverse order we can easily reverse direction of the axes. For example if we set X-axis limits from maximum value to minimum value it will reverse X-axis. Similarly reversing Y-axis limits will flip Y-axis.

  • xlim(start, end): Sets X-axis range. Reverses it when start > end.
  • ylim(start, end): Same as above for Y-axis.
  • np.linspace(0, 5, 100): Generates 100 evenly spaced numbers between 0 and 5.
Python
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 5, 100)
y = np.exp(x) 
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
ax1.plot(x, y, 'b-', linewidth=2)
ax1.set_title('Original Axes', fontsize=12)
ax1.set_xlabel('X-axis', fontsize=10)
ax1.set_ylabel('Y-axis', fontsize=10)
ax1.grid(True, linestyle='--', alpha=0.7)
ax2.plot(x, y, 'b-', linewidth=2)
ax2.set_xlim(max(x), min(x)) 
ax2.set_ylim(max(y), min(y)) 
ax2.set_title('Reversed Axes', fontsize=12)
ax2.set_xlabel('X-axis (Reversed)', fontsize=10)
ax2.set_ylabel('Y-axis (Reversed)', fontsize=10)
ax2.grid(True, linestyle='--', alpha=0.7)
ax2.arrow(max(x), min(y), -0.5, 0, head_width=0.5, head_length=0.5, fc='k', ec='k')
ax2.arrow(min(x), max(y), 0.5, 0, head_width=0.5, head_length=0.5, fc='k', ec='k')
plt.tight_layout()
plt.show()

Output:

reverse-2

Normal Plot and Inverted Plot (Both axes inverted)

Note: xlim() and ylim() affect current active axes. If we’re working with multiple subplots ensure we activate correct subplot .

Method 3: Using axis() method

The axis() method in Matplotlib allows us to control limits of both X and Y axes simultaneously. By specifying limits in reverse order we can easily flip the axes. This method takes a list of four values: [xmin, xmax, ymin, ymax] where we can set minimum and maximum values for both axes.

  • plt.axis([max(x), min(x), max(y), min(y)]): Reverses X and Y axes by setting their limits in reverse order, flipping direction of both axes.
  • np.linspace(-5, 5, 100): Generates 100 evenly spaced numbers between -5 to 5.
Python
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5, 5, 100)
y = np.abs(x)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 5))
ax1.plot(x, y, 'b-', linewidth=2)
ax1.set_title('Original Axes', fontsize=12)
ax1.set_xlabel('X-axis', fontsize=10)
ax1.set_ylabel('Y-axis', fontsize=10)
ax1.grid(True, linestyle='--', alpha=0.7)
ax2.plot(x, y, 'b-', linewidth=2)
ax2.axis([max(x), min(x), max(y), min(y)])
ax2.set_title('Reversed Axes', fontsize=12)
ax2.set_xlabel('X-axis (Reversed)', fontsize=10)
ax2.set_ylabel('Y-axis (Reversed)', fontsize=10)
ax2.grid(True, linestyle='--', alpha=0.7)
plt.tight_layout()
plt.show()

Output:

reverse-3

Normal Plot and Inverted Plot (using axis())

These methods provide simple and effective ways to reverse the X, Y or both axes in Matplotlib offering flexibility in how we display our data.



Next Article

Similar Reads