How can I reverse the axes in Matplotlib? 2

How can I reverse the axes in Matplotlib?

To reverse the axes in Matplotlib, one can use the “invert_xaxis()” and “invert_yaxis()” functions. These functions can be called on the respective axes objects and will reverse the direction of the axis ticks and labels. This can be useful when visualizing data in a different orientation or when comparing data with a different baseline. Additionally, the “set_xlim()” and “set_ylim()” functions can be used to manually set the limits of the axes in the reversed orientation. Overall, reversing the axes in Matplotlib allows for greater flexibility in visualizing data and can enhance the overall presentation of a plot.

Reverse Axes in Matplotlib (With Examples)


You can use the following basic syntax to reverse the x-axis and y-axis in Matplotlib:

plt.gca().invert_xaxis()
plt.gca().invert_yaxis()

The following example shows how to use this syntax in practice.

Example: Reverse Axes in Matplotlib

The following code shows how to create a basic scatterplot in Matplotlib:

import matplotlib.pyplotas plt

#define x and y
x = [1, 4, 8, 11, 13, 14]
y = [5, 11, 18, 26, 25, 23]

#create scatterplot of x and y
plt.scatter(x, y)

We can use the following code to reverse the y-axis:

import matplotlib.pyplotas plt

#define x and y
x = [1, 4, 8, 11, 13, 14]
y = [5, 11, 18, 26, 25, 23]

#create scatterplot of x and y
plt.scatter(x, y)

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

Notice that the y-axis now ranges from 25 to 5 instead of 5 to 25.

Alternatively, we could use the following code to reverse the x-axis:

import matplotlib.pyplotas plt

#define x and y
x = [1, 4, 8, 11, 13, 14]
y = [5, 11, 18, 26, 25, 23]

#create scatterplot of x and y
plt.scatter(x, y)

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

Notice that the x-axis now ranges from 14 to 0 instead of 0 to 14.

Lastly, we could use the following code to reverse both axes:

import matplotlib.pyplotas plt

#define x and y
x = [1, 4, 8, 11, 13, 14]
y = [5, 11, 18, 26, 25, 23]

#create scatterplot of x and y
plt.scatter(x, y)

#reverse both axes
plt.gca().invert_xaxis()
plt.gca().invert_yaxis()

Notice that both axes values are reversed.

Additional Resources

Cite this article

stats writer (2024). How can I reverse the axes in Matplotlib?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-reverse-the-axes-in-matplotlib/

stats writer. "How can I reverse the axes in Matplotlib?." PSYCHOLOGICAL SCALES, 2 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-i-reverse-the-axes-in-matplotlib/.

stats writer. "How can I reverse the axes in Matplotlib?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-reverse-the-axes-in-matplotlib/.

stats writer (2024) 'How can I reverse the axes in Matplotlib?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-reverse-the-axes-in-matplotlib/.

[1] stats writer, "How can I reverse the axes in Matplotlib?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.

stats writer. How can I reverse the axes in Matplotlib?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top