HomeLinuxMatplotlib Invert Y-Axis

Matplotlib Invert Y-Axis


In Python, a number of libraries exist which can be utilized for plotting the graphs for knowledge visualization. By utilizing these library capabilities/strategies, customers can flip or revert the axes of the plot. For that corresponding objective, the “matplotlib” library is commonly used.

The outcomes of this weblog are:

How one can Use Matplotlib “invert_yaxis()” Perform to Invert Y-Axis in Python?

The matplotlib library’s “invert_yaxis()” operate can be utilized to invert or reverse the y-axis place in Python. For this objective, first, import the required libraries, comparable to “matplotlib.pyplot” and “numpy”:

import matplotlib.pyplot as plt
import numpy as np

Subsequent, apply the numpy library’s “linspace()” operate to generate the x-series knowledge factors. Then, use the “sin()” operate and cross it to the “x” variable that comprises the generated x collection knowledge factors. That may retrieve the worth of the angle in levels and radians. After that, retailer into the “y” variable:

x = np.linspace(5, 15, 35)
y = np.sin(x)

Now, use the “plt.plot()” technique that comprises the “x” and “y” as an argument. Then, apply the “plt.gca()” operate which is a module of the “matplotlib” library to retrieve the axes situations on the present graph matching the offered key phrases “arg”, or create one. Within the subsequent line, now we have invoked the “plt.title()” technique and specified the plot title. Lastly, name the “plt.present()” technique to show the resultant determine:

plt.plot(x, y)
ax = plt.gca()
plt.title(“Non-Inverted Plot”)
plt.present()

Consequently, the graph has been displayed with out an inverted y-axis:

Now, to revert the y-axis, apply the “invert_yaxis()”. Then, specify the graph title by utilizing the “plt.title()” technique and name the “plt.present()” technique to point out the determine:

ax.invert_yaxis()
plt.title(“Inverted Plot”)
plt.present()

In accordance with the next output, the y-axis of the graph has been reverted efficiently:

How one can Use Matplotlib “plt.ylim()” Methodology to Invert Y-Axis in Python?

The matplotlib library’s “plt.ylim()” technique can also be used for inverting the y-axis of the graph in Python. To take action, import the beneath given Python libraries:

import matplotlib.pyplot as plt
import numpy as np

Then, specify the “linspace()” operate to create the x-series knowledge factors. Subsequent, use the equation for a straight line as we have to plot a straight line within the graph that has its origin on the y-axis and reserve it into the “y” variable:

x = np.linspace(5, 15, 35)
y = 3*x+4

Now, apply the “plt.plot()” technique that’s used to attract the factors in a graph:

After that, specify the plot title with the assistance of the “plt.title()” technique. Now, name the “plt.ylim()” technique to flip the specified axis of the graph. Right here, now we have handed the “max” and “min” parameters to this operate. Then, invoke the “plt.present()” technique to get the resultant plot determine:

plt.title(“Inverted Plot”)
plt.ylim(max(y), min(y))
plt.present()

Output

How one can Use Matplotlib “plt.axis()” Methodology to Invert Y-Axis in Python?

By utilizing the “plt.axis()” technique, customers can revert the axis of the graph. For this corresponding objective, use the beforehand described code after which, apply the “plt.axis()” technique having each axes “max” and “min” values:

plt.axis([max(x), min(x), max(y), min(y)])

You’ve gotten realized concerning the completely different strategies for inverting the matplotlib y-axis in Python.

Conclusion

To revert the graph’s y-axis in Python, the “matplotlib” library offered a number of strategies, such because the “invert_yaxis()” technique, “plt.ylim()” technique, and the “plt.axis()” technique. This information illustrated the “matplotlib” library strategies for inverting the y-axis in Python.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments