What is add_axes matplotlib

In this Python Matplotlib tutorial, we’ll discuss the add_axes matplotlib. Here we will cover different examples related to add_axes using matplotlib in Python. And we’ll also cover the following topics:

  • add_axes matplotlib
  • add_axes rect matplotlib
  • add_axes on each other matplotlib
  • add_axes polar matplotlib
  • add_axes projection matplotlib
  • add_axes scatter matplotlib
  • add_axes facecolor matplotlib
  • add_axes multiple bars matplotlib
  • add_axes plot inside plot matplotlib

add_axes matplotlib

 Matplotlib is a library used for data visualization in Python. The figure module provides the figure, which has all the elements of the plot. To add the axes to the figure we use the figure module of the matplotlib library.

The function of the figure module used to add axes to the figure is add_axes().

The following is the syntax:

matplotlib.Figure.figure.add_axes(*args, *kwargs)

The parameters are as follow:

ParameterValueDescription
rect[left, bottom, width, height]The parameter is used to set new dimensions of the figure. It takes values in float also.
projectionNone, ‘aitoff’, ‘hammer’, ‘lambert’, ‘mollweide’, ‘polar’, ‘rectilinear’The parameter is used to set a new projection of axes. By default, the project is None which results in ‘rectilinear’ projection.
polarboolBy default, polar is set to False. If we set it to True, axes results in ‘polar’ projection.
sharex or shareyaxesThe parameter is used to share the x-axis and y-axis.

Also, check: How to install matplotlib python

add_axes rect matplotlib

Here we’ll see an example of the add_axes function with rect parameter in Matplotlib. In this example, we only add single axes.

The following is the syntax:

matplotlib.figure.figure.add_axes(rect=[])

Example:

# Import library

import matplotlib
  
# Create figure() objects

fig = matplotlib.pyplot.figure()
  
# Creating axis

axes = fig.add_axes([0.75, 1, 0.5, 1])
  
# Display

plt.show()
  • First, import matplotlib.pyplot library.
  • Next, create figure() module.
  • To add axes to the figure,use the add_axes() function.
  • Here we set xmin, ymin, width, and height to 0.75, 1, 0.5, 1 respectively.
  • To display the plot, use the show() function.
add_axes rect matplotlib
add_axes()

Read: Matplotlib plot a line

add_axes on each other matplotlib

Here we’ll see an example where we add a plot above each other by using the add_axes matplotlib function.

Example:

# Import libraries

import matplotlib
import numpy as np
  
# Create figure() objects

fig = matplotlib.pyplot.figure()
  
# Generate line graph

x = np.arange(0, 20, 0.2)
y1 = np.sin(x)
y2 = np.sqrt(x)
  
# Creating two axes

axes1 = fig.add_axes([0, 0, 1, 1])
axes1.plot(x, y1)
axes2 = fig.add_axes([0, 1, 1, 1])
axes2.plot(x, y2)
  
# Show plot

plt.show()
  • Import matplotlib.pyplot and numpy libraries.
  • Next, we create figure() object.
  • After this, we define data coordinates using arange(), sin(), and sqrt() functions of numpy.
  • Then we create two axes, using add_axes() function.
add_axes on each other matplotlib
add_axes()

Read: Python plot multiple lines

add_axes polar matplotlib

By using the add_axes() function of the figure module, we can plot the curves in polar coordinates. For this, we pass polar as a parameter and set its value to True.

The following is the syntax:

matplotlib.figure.figure.add_axes(rect,polar)

Let’s see an example:

# Import libraries

import matplotlib
import numpy as np
  
# Create figure() objects

fig = matplotlib.pyplot.figure()
  
# Generate graph

x = np.arange(0, 20, 0.2)
y1 = np.exp(x)
y2 = np.sqrt(x)
  
# Creating two axes

axes1 = fig.add_axes([0, 0, 1, 1],polar=True)
axes1.plot(x, y1)
axes2 = fig.add_axes([1, 1, 1, 1])
axes2.plot(x, y2)
  
# Show plot

plt.show()
  • First, we import important libraries such as matplotlib.pyplot and numpy.
  • Next, we create figure object by using figure() function.
  • We define data coordinates using arange(), exp() and sqrt() functions.
  • Next, we create first axes using add_axes() function and to create plot In polar curves we pass polar as parameter and set its value to True.
  • To create second axes, we again use add_axes() function and we also set its position by passing rect parameter.
add_axes polar matplotlib
add_axes(polar=True)

Read: Matplotlib plot bar chart

add_axes projection matplotlib

To set a new projection on the axes, we pass the projection argument to the add_axes() function in matplotlib.

The syntax is as below:

matplotlib.figure.figure.add_axes(rect, projection)

Example:

# Import libraries

import matplotlib
import numpy as np
  
# Create figure() objects

fig = matplotlib.pyplot.figure()
  
# Generate graph

x = np.arange(0, 5, 0.1)
y1 = np.trunc(x)
y2 = np.arctan(x)
  
# Creating two axes

axes1 = fig.add_axes([0, 1, 1, 1],projection='hammer')
axes1.plot(x, y1)
axes2 = fig.add_axes([1, 1, 1, 1],projection='polar')
axes2.plot(x, y2)
  
# Show plot

plt.show()
  • Import libraries matplotlib.pyplot and numpy.
  • Next, we create figure object using figure() function.
  • To define data points, we use arange(), trunc() and arctan() functions of numpy.
  • To generate plot, use plot() function.
  • To add axes to the figure, use add_axes() function.
  • To set the new projection to the axes, we pass projection argument to method and set their value to hammer and polar respectively.
add_axes projection matplotlib
add_axes(projection)

Read: What is matplotlib inline

add_axes scatter matplotlib

Here we learn to add axes by using add_axes to the scatter plot. To generate a scatter graph, use the scatter() function in Matplotlib.

Example:

# Import libraries

import matplotlib
import numpy as np
  
# Create figure() objects

fig = matplotlib.pyplot.figure()
  
# Generate line graph

x = np.arange(0, 20, 0.2)
y1 = np.sin(x)
y2 = np.arctan(x)
  
# Creating two axes

axes1 = fig.add_axes([0, 1, 1, 1])
axes1.scatter(x, y1)
axes2 = fig.add_axes([1, 0, 1, 1])
axes2.scatter(x, y2)
  
# Show plot

plt.show()
add_axes scatter matplotlib
Scatter plot – scatter()

Read: Matplotlib scatter plot color

add_axes facecolor matplotlib

Here we’ll see an example where we add axes to the figure by using the add_axes method and we also set the background color of the figure by using the set_facecolor method in Matplotlib.

# Import libraries

import matplotlib
import numpy as np
  
# Create figure() objects

fig = matplotlib.pyplot.figure()
  
# Generate line graph

x = np.linspace(20, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

# Set facecolor

fig.set_facecolor('mistyrose')
  
# Creating two axes


axes1 = fig.add_axes([0, 0, 1, 1])
axes1.plot(x, y1)
axes2 = fig.add_axes([1, 1, 1, 1])
axes2.plot(x, y2)
  
# Show plot

plt.show()
  • Here we define data coordinates using linspace(), sin() and cos() function of numpy.
  • To set the facecolor we use set_facecolor() function and set it to mistyrose.
add_axes facecolor matplotlib
set_facecolor()

Read: Matplotlib scatter plot legend

add_axes multiple bars matplotlib

Here we’ll see an example where we’ll add_axes to multiple bars figure using Matplotlib.

Example:

# Import libraries

import matplotlib
import numpy as np
  
# Create figure() objects

fig = matplotlib.pyplot.figure()
  
# Generate graph

x = [2, 4, 6, 8, 10]
y1 = [3, 5.5, 7, 12, 3]
y2 = [2.3, 5, 6, 10, 15]
  
# Creating two axes

axes1 = fig.add_axes([1, 1, 1, 1])
axes1.bar(x, y1)
axes2 = fig.add_axes([1, 0, 1, 1])
axes2.bar(x,y2)
  
# Show plot

plt.show()
  • To plot a bar chart, use bar() method.
  • To add axes to the figure, use add_axes() method.
add_axes multiple bars matplotlib
bar()

Also, check: Matplotlib multiple bar chart

add_axes plot inside plot matplotlib

Here we’ll see an example, where we add a plot inside a plot using the add_axes() method.

Example:

# Import libraries

import matplotlib
import numpy as np
  
# Create figure() objects


fig = matplotlib.pyplot.figure()

# Define Data

x = [3, 6, 9, 12, 15]
y = [5.5, 8, 10.5, 23, 12]

# Plot

plt.plot(x,y)

# add plot inside a plot

axes=fig.add_axes([0.20, 0.48, 0.28, 0.28])
axes.plot([1,5])

# display

plt.show()
  • Firstly, we import matplotlib.pyplot, and numpy library.
  • To create a figure object, use the figure() method.
  • To define data points x and y.
  • To plot a graph, use the plot() method of pyplot module.
  • To add a plot inside an already generated plot, use the add_axes() method.
  • After generating a plot, use the plot() method to plot a graph.
  • To visualize the graphs, use the show() method.
add_axes plot inside plot matplotlib
# Plot Inside Plot

You may also like to read the following matplotlib tutorials.

So, in this Python tutorial, we have discussed the “add_axes matplotlib” and we have also covered some examples related to using add_axes matplotlib. These are the following topics that we have discussed in this tutorial.

  • add_axes matplotlib
  • add_axes rect matplotlib
  • add_axes on each other matplotlib
  • add_axes polar matplotlib
  • add_axes projection matplotlib
  • add_axes scatter matplotlib
  • add_axes facecolor matplotlib
  • add_axes multiple bars matplotlib
  • add_axes plot inside plot matplotlib