Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flip a plot in matplotlib

I am creating a plot with matplotlib

import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0, 5, 0.1);
y = np.sin(x)
plt.plot(x, y)

Can I flip the plot, making the y-axis inverted and all positive values negative and vice versa?

I know I can multiply by -1 and use invert_yaxis but I wonder if there is a function for flipping it without changing the values.

like image 932
Jamgreen Avatar asked Apr 27 '15 22:04

Jamgreen


People also ask

How do I flip a plot in matplotlib?

Matplotlib invert x and y axis By using invert_xaxis() and invert_yaxis() method we can flip the x-axis and y-axis respectively. In the above example, we import numpy and matplotlib libraries.

How do you flip a graph in Python?

Most common method is by using invert_xaxis() and invert_yaxis() for the axes objects. Other than that we can also use xlim() and ylim(), and axis() methods for the pyplot object. To invert X-axis and Y-axis, we can use invert_xaxis() and invert_yaxis() function.

What does PLT axis () do?

The plt. axis() method allows you to set the x and y limits with a single call, by passing a list which specifies [xmin, xmax, ymin, ymax] : In [11]: plt.


1 Answers

Try the following function:

plt.gca().invert_yaxis()
like image 198
A.J. Uppal Avatar answered Sep 30 '22 04:09

A.J. Uppal