# Matplotlib invert y axis

In this Python tutorial, we will discuss Matplotlib invert y axis in python. Here we will cover different examples related to inverting of the y-axis using matplotlib. And we will also cover the following topics:

• Matplotlib invert y axis
• Matplotlib invert y axis subplots
• Matplotlib invert x and y axis
• Matplotlib barh invert y axis
• Matplotlib invert secondary y axis
• Matplotlib 3D invert y axis
• Matplotlib flip y axis label
• Matplotlib invert y axis imshow

If you are new to python Matplotlir, check out, How to install matplotlib python.

## Matplotlib invert y axis

In this section, we learn about how to invert the y-axis in matplotlib in Python. Now before starting the topic firstly, we discuss what does invert mean here:

Invert means reverse or flip the order

To understand more clearly let’s take a common example:

Suppose the y-axis starts from 0 to 10 and you want to start it from 10 to 0. So in that cases, we invert or flip the axes of the plot.

In matplotlib, we can invert the y-axis of a graph using different methods. The following are the different methods used for reversing the y-axis are as below.

• Using invert_yaxis() method
• Using ylim() method
• Using axis() method

### By using invert_yaxis() method

To invert Y-axis, we can use invert_yaxis() method.

Syntax of the method is as below:

``matplotlib.axes.Axes.invert_yaxis(self)``

Example: (Without inverting axis)

``````# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.arange(0, 15, 0.2)
y = np.sin(x)

# Plot figure

plt.plot(x, y)

# Title

plt.title("Sine Function Normal Plot", fontsize= 15, fontweight='bold')

# Generate Plot

plt.show()``````
• In the above example, we import the matplotlib.pyplot and numpy library.
• After this we define data using np.arrange() and np.sin() method.
• Next, we use the plt.plot() method to draw the plot.
• By using plt.title() we define the title of the plot.
• plt.show() method is used to generate the plot on the user screen.

Look at the y-axis: Here it starts from -1.00 and ends at 1.00

Example: (With inverting axis)

``````# Import Library

import matplotlib.pyplot as plt
import numpy as np

# Define Data

x = np.arange(0, 15, 0.2)
y = np.sin(x)

# Plot figure

plt.plot(x, y)

# Invert y-axis

ax = plt.gca()
ax.invert_yaxis()

# Title

plt.title("Sine Function Invert Plot", fontsize= 15, fontweight='bold')

# Generate Plot

plt.show()``````
• In the above example, we include the plt.gca() method to get current axes.
• After this, we use the ax.invert_yaxis() method to invert or reverse the y-axis of the plot.

Look at the y-axis: Here it starts from 1.00 and ends at -1.00

Conclusion: By using invert_yaxis() method we can reverse the y-axis of the plot.

### By using ylim() method

ylim() method is also used to invert axes of a plot in Matplotlib. Generally, this method is used to set limits for the axes.

But if we set the minimum value as the upper limit and maximum value as the lower limit we can revert the axes.

The syntax of the ylim() method is as below:

``matplotlib.pyplot.ylim(bottom,top)``

Here bottom and top specify the tuple of the new y-axis limits.

Let’s see an example to understand it more clearly:

``````# Import Library

import numpy as np
import matplotlib.pyplot as plt

# Define Data

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

# Create plot

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

# Normal plot

p1.plot(x, y)
p1.set_title("Normal Plot")

# Invert plot

p2.plot(x, y)
p2.set_title("Inverted Plot")
plt.ylim(max(y), min(y))

# Show

axes.tight_layout()
plt.show()``````
• In the above example, firstly we import numpy and matplotlib.pyplot library.
• Next, we define the data and by using the plt.subplot() method we create subplots.
• Now we use the plot() method to draw a simple plot.
• plt.ylim() method is used to invert the y-axis here we pass max and min as a parameter.

Conclusion: In the normal plot, the y-axis starts from 1 and ends at 5. And In the inverted plot, the y-axis starts from 5 and ends at 1.

### By using axis() method

The axis() method is also used to revert axes in Matplotlib. Basically, this method is used to set the minimum and maximum values of the axes.

But if we set the minimum value as the upper limit and the maximum value as the lower limit we can get inverted axes.

The syntax of the axis() method is as below:

``matplotlib.pyplot.axis()``

Let’ see an example:

``````# Import Library

import numpy as np
import matplotlib.pyplot as plt

# Define Data

x = np.arange(0, 15, 0.2)
y = np.tan(x)

# Create plot

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

# Normal plot

p1.plot(x, y)
p1.set_title("Normal Plot")

# Invert plot

p2.plot(x, y)
p2.set_title("Inverted Plot")
plt.axis([max(x), min(x), max(y), min(y)])

# Show

axes.tight_layout()
plt.show()``````
• Here we import matplotlib.pyplot and numpy library and define data using np.arrange() and np.tan() method.
• plt.subplots() method is used for creating subplots.
• Then we use the plot() method to plot a graph and the set_title() method is used to add a title to the plot.
• By using the plt.axis() method we revert the axes and here we pass the max and min values of the y and x axes.

## Matplotlib invert y axis subplots

Here we will discuss how to invert the y-axis of the specific subplot if we draw multiple plots in a figure area in Python matplotlib.

We use the invert_yaxis() method to flip the y-axis of the subplot.

Let’s understand the concept with the help of an example:

``````# Import Libraries

import numpy as np
import matplotlib.pyplot as plt

# Define Data

x1= [0.2, 0.4, 0.6, 0.8, 1]
y1= [0.3, 0.6, 0.8, 0.9, 1.5]

x2= [2, 6, 7, 9, 10]
y2= [3, 4, 6, 9, 12]

x3= [5, 8, 12]
y3= [3, 6, 9]

x4= [5, 8, 12]
y4= [3, 6, 9]

fig, ax = plt.subplots(2, 2)

# Invert y-axis

ax[1,0].invert_yaxis()

# Plot graph

ax[0, 0].plot(x1, y1)
ax[0, 1].plot(x2, y2)
ax[1, 0].plot(x3, y3)
ax[1, 1].plot(x4, y4)

# Display Graph

fig.tight_layout()
plt.show()``````
• In the above example, we plot multiple plots in a figure area. And we want to invert the y-axis of the specific plot.
• Here we use the invert_yaixs() method to flip the y-axis of the plot.
• We use the invert_yaxis() method with the third plot.

## Matplotlib invert x and y axis

Here we are going to learn how to invert the x-axis and y-axis of the plot in Python matplotlib.

By using invert_xaxis() and invert_yaxis() method we can flip the x-axis and y-axis respectively.

The syntax is as given below:

``````# Invert x-axis

matplotlib.axes.Axes.invert_xaxis(self)

# Invert y-axis

matplotlib.axes.Axes.invert_yaxis(self) ``````

Let’s see an example related to this concept:

``````# Import Library

import numpy as np
import matplotlib.pyplot as plt

# Define Data

x=[5, 10, 15, 20, 25,30]
y=[0, 1, 2, 3, 4, 5]

# Create Subplot

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

# Normal plot

p1.plot(x, y)
p1.set_title("Normal Plot")

# Invert plot

p2.plot(x, y)
p2.set_title("Inverted Plot")

# Invert axes

ax = plt.gca()
ax.invert_yaxis()
ax.invert_xaxis()

# Show

axes.tight_layout()
plt.show()``````
• In the above example, we import numpy and matplotlib libraries.
• After this, we define data on x and y coordinates.
• By using the plt.subplots() method we create subplots in a figure area.
• Next, we use the plot() method to plot a graph and the set_title() method to set the title of the plot.
• Then we use plt.gca() method to get current axes of the plot.
• By using the invert_yaxis() and the invert_xaxis() method we flip the axes of the plot.
• In last, we use the tight_layout() method to automatically set the plots and the show() method to display the plot on the user’s screen.

Also, read Matplotlib savefig blank image

## Matplotlib barh invert y axis

Here we are going to learn how we invert the y-axis of the horizontal bar chart using matplotlib in Python. Firstly, we have to know how to plot a horizontal bar chart.

The syntax to plot the horizontal bar chart is as below:

``matplotlib.pyplot.barh(x,y)``

Here x represents the x-axis coordinates and y represents the height of the bars.

The syntax to invert the y-axis of the barh chart is as given below:

``````# Invert y-axis

matplotlib.axes.Axes.invert_yaxis(self)  ``````

Example:

``````# Import Library

import numpy as np
import matplotlib.pyplot as plt

# Define Data

x=[1, 2, 3, 4, 5]
y=[20, 15, 12, 6, 5]

# Simple Bar Chart

plt.figure()
plt.barh(x,y)

# Inverted Bar chart

plt.figure()
plt.barh(x,y)
plt.gca().invert_yaxis()

# Show

plt.show()``````
• In the above example, we first import the numpy and matplotlib.pyplot library.
• Next, we define data.
• By using the plt.barh() method we create a horizontal bar chart.
• By using the plt.gca() method we get the current axes of the plot.
• Then we use the invert_yaxis() method to flip the y-axis of the plot.

## Matplotlib invert secondary y axis

Here we are going to learn how we can invert the secondary y-axis in matplotlib in Python. Firstly, we have to understand what does secondary y-axis means and when we need it.

Once in a while, we need two x-axes or y-axes to get more insights into the data. At that moment we need to create secondary axes.

In python, matplotlib provides the functionality to create a plot that has two y-axes, and even we can provide different labels to both.

We can create a plot with two different y-axes by using two different axes objects. And to create different axes objects we use the twinx() method.

Let’s see an example where we invert the secondary y-axis:

``````# Import Library

import numpy as np
import matplotlib.pyplot as plt

# Define Data

x = np.arange(0, 10, 3.5)
y1 = x**2
y2 = x**4

# Create subplot

fig, ax = plt.subplots(figsize = (10, 5))

# twinx() for creating axes object for secondary y-axis

ax2 = ax.twinx()

# Plot graph

ax.plot(x, y1)
ax2.plot(x, y2)

# Invert secondary y-axis

ax2.invert_yaxis()

# Labels to axes

ax.set_xlabel('x-axis')
ax.set_ylabel('y-axis')

# Label secondary y-axis

ax2.set_ylabel('Secondary y-axis', color = 'b')

# Display layout

plt.tight_layout()

# Show plot

plt.show()``````
• In the above example, we import important libraries such as numpy and matplotlib.pyplot.
• Next, we create data by using the np.arange() method here we define data between 0 to 10 with the difference of 3.5 at the x-axis.
• Then by using plt.subplots() methods we create subplots.
• By using the twinx() we create another axes object for the secondary y-axis.
• set_xlabel() and set_ylabel() methods are used for adding labels on the axes.
• By using the tight_layout() and show() method we define the layout and show the plot.

## Matplotlib 3D invert y axis

In this section, we are going to study how to invert a 3D plot in Matplotlib in Python.

The syntax to create a 3D plot and invert the y-axis:

``````# Create 3D plot
ax.plot3D(x,y,z)

# Invert y-axis
matplotlib.pyplot.ylim(max(y),min(y))``````

Here x, y, and z are the coordinates points.

Let’s see an example related to this:

``````# Import Libraries

import matplotlib
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

# Create subplots

fig = plt.figure(figsize=(12,5), dpi=80)

# Simple plot

x=[1,1,10,10]
y=[1,10,10,10]
z=[1,1,1,10]
ax1.plot(x,y ,z , marker="o")

# Inverted Plot

x1=[1,1,10,10]
y1=[1,10,10,10]
z1= [1,1,1,10]

# Function used for invert y-axis

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

ax2.plot(x1,y1 ,z1, marker="o")

# Show Plot

plt.show()
``````
• In the above example, we import important libraries such as matplotlib.pyplot, and mplot3D.
• Next, we use the plot() method to plot a graph in a figure area.
• By using the ylim() method we invert the y-axis of the plot. In function, we pass the max and min of y1 coordinates.

## Matplotlib flip y axis label

Here we are going to learn to flip labels at the y-axis in Matplotlib in Python. By using the ylabel() method we can add the label at the y-axis and flip it.

Example:

``````# Import Library

from matplotlib import pyplot as plt

# Define Data

x=[0, 1, 2, 3, 4, 5]
y=[2, 4, 6, 8, 10, 12]

# Plot graph

plt.plot(x,y)

# Flip Y-axis Label

plt.ylabel('Y-axis',rotation=0)

# Show

plt.draw()``````
• In the above example, we import matplotlib.pyplot library for visualization.
• Next, we define the data and use the plt.plot() method to draw a plot.
• By using plt.ylabel() method we set the label at the y-axis and flip it horizontally by passing the rotation argument. Here we set the value of rotation to 0.

## Matplotlib invert y axis imshow

Here we learn to invert the y-axis in the imshow() method in Python Matplotlib. Firstly, understand what does imshow does:

inshow() method shows an image of a color-mapped or 3D RGB array.

The syntax of the inshow() method is given below:

``````matplotlib.pyplot.imshow(X,
cmap=None,
norm=None,
aspect=None,
interpolation=None,
alpha=None,
vmin=None,
vmax=None,
origin=None,
extent=None,
shape=,
filternorm=1,
imlim=,
resample=None,
url=None,
data=None,
*kwargs)``````

The parameters used above are described as below:

• X: specify data of the image.
• cmap: specifies colormap or registered colormap name.
• norm: It is a normalized instance used to scale data.
• aspect: Used to control the aspect ratio of the axes.
• interpolation: It is an interpolation method used to display images.
• alpha: specify the intensity of the color.
• vmin, vmax: specify the range of the color bar.
• origin: Used to place [0,0] index of the array in the upper left or lower corner of the axes.
• extent: It is a bounding box in data coordinates.
• filternorm: Used for the antigrain image resize filter.
• url: set the URL of the axes image.

Example:

``````# Import Libraries

import numpy as np
from matplotlib import pyplot as plt

# Define data

x = np.array([[0, 1, 2, 3],
[1, 1, 1, 3],
[1, 2, 2, 3],
[2, 2, 3, 3]])

# Create subplot

fig, ax = plt.subplots(1,2)

# Imshow and Invert Y-axis

for i in range(2):
ax.imshow(x, cmap = 'summer', vmin = 1, vmax = 3,)
ax.imshow(x, cmap = 'summer', vmin = 1, vmax = 3,
origin='lower')
# Show

plt.show()``````
• Here we define data using the np.array() method and use the imshow() method to plot the image of the colormap.
• We pass the parameter origin to flip the y-axis and set its value to lower.