In this post, we will learn How to Invert the x or y-axis which we can perform using matplotlib library where there are methods like set_xlim(), and set_ylim().
Inverting The Axis
Here we have given an example that we can directly use as it is to convert x axis into y axis and visa versa.
import matplotlib.pyplot as plt # Here we create some data to plot x = [1, 2, 3, 4, 5] y = [10, 20, 30, 40, 50] # Here we create a plot with default x and y limits plt.plot( x, y) # For convert the x-axis plt.gca().convert_xaxis() # for convert the y-axis plt.gca() .convert_yaxis() # show the plot plt.show()
Here we first created a plot by using plt. plot() and there the plot we created has default dimensions that are horizontal said to be the x axis and vertical zones are called the y-axis. And here we created another function convert_ x axis () method on the current axis and here ‘gca’ means get the current axis we did the same thing for converting the y-axis which is done by converting the y-axis () method.
Here we need to keep the certain things in mind we can always modify the limit of the axis further too by simply passing in the arguments for the ‘set xlim()’ and ‘set ylim()’ to understand it better we have given an example below where we can how does it work. And here covering means making x axis as y axis and y axis as x axis.
plt.gca(). convert _yaxis() plt. ylim(100, 0)
We need to note certain things here is the approach we took in this example works only in matplotlib plots and might face some problems when we apply the same approach with different other visualizations and libraries.
We can also do the same thing using the Numpy library and the example to implement using it is given below where we can find how it works.
import numpy as np import matplotlib.pyplot as plt # create some data to plot the data x = np.array([1, 2, 3, 4, 5]) y = np.array([10, 20, 30, 40, 50]) # create a plot with default x and y limits plt.plot( x, y) # convert the x-axis of the given plot plt.plot(x[::-1], y) # convert the y-axis of the given plot plt.plot(x, y[::-1]) # show the converted plot plt.show()
To learn more about How to convert the x or y axis visit: by geeks for geeks.
To learn more about Python solutions to different Python problems and tutorials for the concepts we need to know to work on Python programming along with different ways to solve any generally asked problems: How To Pass-Variables From A Java & Python Client To A Linux/Ubuntu Server Which Is Running C?.