Python

# Matplotlib Invert Y-Axis

The plot’s axes are reversed or flipped in Matplotlib. There are multiple ways exist for flipping the y-axis of a figure. For the axes elements, the most popular technique is to use the ­invert_yaxis() function. Aside from that, we could utilize the ylim() and axis() functions of the matplotlib object.

In this article, we will go through how to use Matplotlib to reverse the y-axis in detail, and further, we discuss alternative techniques used for flipping the y-axis with Matplotlib.

## Use invert_yaxis() Function

To reverse Y-axis, we could utilize the invert_yaxis() technique. With the help of this methodology, we will reverse either one or both of the dimensions.

import matplotlib.pyplot as plt

import numpy as np

a = np.linspace(10, 25, 40)

b = 5*a+6

graph, (plot1, plot2) = plt.subplots(1, 2)

plot1.plot(a, b)

plot1.set_title("Original Graph")

plot2.plot(a, b)

plot2.set_title("Inverted Graph")

plot2.invert_xaxis()

plot2.invert_yaxis()

graph.tight_layout()

plt.show() At the start of the code, we integrate Matplotlib and NumPy libraries. Now, we have to generate data points of the x series. This can be done by using the linspace() function of the NumPy library. We apply the equation for a straight line as we want to draw a straight line in the graph. This straight line has its origin at the y-axis.

In addition to this, we draw space or gap for two plots by the use of plt.subplots(). In this function, we pass no. of rows and no. of columns as a parameter. Now, we draw the first graph which has normal axes. So, we call plot1.plot() function. Now to set the title of that plot, we apply plot1.set_title() method.

In the same way, to draw a second plot that has reversed axes we define plot2.plot() function. Further, we specify the title for the second graph so we call the set_title() function for this graph also. Now, we reverse data points of the x-axis and y-axis by using the invert_axis() method. ## Use ylim() Function

In Matplotlib, the ylim() technique can also be utilized to flip the dimensions of a plot. In most cases, this procedure is being used to define axis limitations.

import matplotlib.pyplot as plt

import numpy as np

a = np.linspace(15, 25, 45)

b = 3*a+7

axes,(p1,p2) = plt.subplots(1, 2)

p1.plot(a, b)

p1.set_title("Original Graph")

p2.plot(a, b)

p2.set_title("Reversed Graph")

plt.ylim(max(b), min(b))

axes.tight_layout()

plt.show() First, we introduce matplotlib.pyplot and the NumPy package. Now, we define the data sets with the help of the np.linspace() function. Further, we call plt.subplots() to create graphs. The no. of rows and no. of columns are passed to this function.

Now the function p1.plot() is applied to draw the original graph. Furthermore, we call the p1.set_title() method to set the label of the first graph. Similarly, we call these functions to draw the second graph and specify the title of the second graph. The title of the second graph is defined as ‘Reversed Graph’.

Here, we employ the plt.ylim() function to reverse the y-axis. And we provided ‘max’ and ‘min’ as arguments for this function. At the end of the code, we want to display the graph so we apply the plt.show() function. After executing the aforementioned code, we get two graphs: Original Graph and Reversed Graph. The original plot has a y-axis that begins from 45 and terminates at 85. However, the reversed plot has an inverted y-axis. It starts at 85 and terminates at 45. This is how we invert the axes of the graph.

## Use axis() Function

Just like the ylim () function, the axis () function is also utilized to define the minimum and highest values of the axes. We just pass ‘max’ and ‘min’ arguments to this function in the succeeding code.

import matplotlib.pyplot as plt

import numpy as np

a = np.arange(1, 20, 0.5)

b = np.tan(a)

axes,(p1,p2) = plt.subplots(1, 2)

p1.plot(a, b)

p1.set_title("Original Graph")

p2.plot(a, b)

p2.set_title("Reversed Graph")

plt.axis([max(a), min(a), max(b), min(b)])

axes.tight_layout()

plt.show() Before starting the code, we have to include the required libraries NumPy and matplotlib.pyplot. Now, we create the data sets with the help of arranging() and tan() methods of the NumPy package. Furthermore, for the creation of graphs, we employ the plt.subplots() function.

We draw an original graph and reversed graph by calling the method plot() respectively. We also set the title of both graphs by the use of the set_title() function. In addition to this, we apply the plt.axis() function to reverse the x and y axes.

So, we provide minimum and maximum values of both axes as a parameter of this function. We represent the graph by the use of the plt.show() function in the end. ## Reverse the Y-axis in a Scatterplot

In this step, we are going to show how we flip the y-axis in a scatterplot.

import matplotlib.pyplot as plt

import numpy as np

a = [3, 6, 10, 12, 15, 17]

b = [6, 12, 19, 22, 26, 21]

plt.scatter(a, b)

plt.gca().invert_yaxis() Here, we import matplotlib.pyplot library for graphic visualizations and NumPy library for numerical analysis. Now, take two variables. We set the data sets for the x-axis and y-axis. These data sets are stored in those variables.

Further, we generate a scatterplot so we call the function plt.scatter(). We employ the plt.gca() function to get the existing axes. Now for inverting the y-axis of the graph, we utilize the invert_yaxis() method. ## Conclusion

In this article, we have deliberated different approaches to inverting the y-axis in the graph. First, we use the invert_yaxis() function to reverse the y-axis. Further, we apply ylim() and axis() functions to flip the y-axis of the graph. The ylim() method is utilized to obtain limitations for axes. Generally, ylim() and axis() both functions are applied to define the smallest and highest values of the axes. Whenever we specify the smallest value as the higher limit and the highest value as the minimum limit we will have reversed axes. In the end, we examine how to reverse the y-axis in the scatterplot. 