Signup/Sign In

Matplotlib Axes Class

In this tutorial, we will cover the Axes class in the state-based interface (pyplot module) in Matplotlib Library.

The image's region with the data space is commonly known as Axes object. The flexible and basic unit for creating sub-plots is Axes.

  • With the help of axes, one can plot at any location in the Figure; thus it provides you the creation of sub-plots in a Flexible way.

  • Any Figure that is given may contain many axes, but a given Axes object can only be in a figure.

  • In the case of two-dimension, the Axes contain two axis objects and in the case of 3D, the Axes contain three-axis objects.

  • To add an Axes object to the figure you just need to make a call to add_axes() method.

  • The add_axes() method will return the axes object and adds axes at the position rect [left, bottom, width, height] where all these quantities are present in fractions of figure width and height.

axes() Function

This function is mainly used to create an axes object with argument.

  • The argument is mainly a list of 4 elements [left, bottom, width, height]

Given below is a basic syntax of this function:

axes([left, bottom, width, height])

Let us take a look at the simplest example where we will use the axes() function:

import matplotlib.pyplot as plt 

fig = plt.figure() 
#[left, bottom, width, height] 
ax = plt.axes([0.2, 0.2, 0.9, 0.9]) 

The output for the same is as follows:

Matplotlib Axes class and axes function

Explanation of the above example:

  • In the above code, axes([0.2, 0.2, 0.9, 0.9]), where the first ‘0.2’ indicates the distance between the left side axis and the border of the figure window that is 20%, of the total width of the figure window.

  • The second ‘0.2’ is indicating the distance between the bottom side axis and the border of the figure window that is 20%, of the total height of the figure window.

  • Also, The first ‘0.9’ indicates that the axes width from left to right is 90% and

  • the latter ‘0.9’ indicates the axes height from the bottom to the top is 80%.

Member Functions in Axes Class

In the following section, we have member functions of axes class which are used to add different elements to the plot:

1. ax.legend() Function

To add a legend to the plot figure the legend() method of axes class is used.

Here is the syntax:

ax.legend(handles, labels, loc)

Parameters:

This function takes three parameters:

  1. The parameter labels is used to indicate a sequence of strings and it mainly handles a sequence of Line2D.

  2. The parameter loc can either be a string or an integer which mainly specifies the legend location.

The Location string The Location code
Best 0
upper right 1
upper left 2
lower left 3
lower right 4
Right 5
Center left 6
Center right 7
lower center 8
upper center 9
Center 10

Below we have a basic example where we will use legend() method:

import matplotlib.pyplot as plt 

fig = plt.figure() 

#[left, bottom, width, height] 
ax = plt.axes([0.2, 0.2, 0.8, 0.8]) 

ax.legend(labels = ('label1', 'label2'), loc = 'center right') 

The output for this code is as follows:

Matplotlib Axes class and axes function

2. add_axes() Function

If there is a need then you can also add the axes object to the figure by just by making a call to the add_axes() method (thus it is an alternative method).

This method will return the axes object and adds axes at position [left, bottom, width, height] where all these quantities are in fractions of figure width and height.

Here is the syntax:

add_axes([left, bottom, width, height])

let us cover an example using this method:

import matplotlib.pyplot as plt 

fig = plt.figure() 

#[left, bottom, width, height] 
ax = fig.add_axes([1, 1, 1, 1]) 

The output for the above code example is as follows:

Matplotlib Axes class and axes function

3. ax.plot() Function

It is the most basic method of the axes class that is used to plot values of one array versus another as lines or markers.

This method can have an optional format string argument which is mainly used to specify color, style and size of line and marker.

Using Color codes:

To specify colors we will use color codes:

Character Color
‘b’ Blue
‘g’ Green
‘r’ Red
‘b’ Blue
‘c’ Cyan
‘m’ Magenta
‘y’ Yellow
‘k’ Black
‘b’ Blue
‘w’ White

Using Marker codes:

To specify style of marker we will use this:

Character Description
‘.’ Point marker
‘o’ Circle marker
‘x’ X marker
‘D’ Diamond marker
‘H’ Hexagon marker
‘s’ Square marker
‘+’ Plus marker

Using Line styles:

Various line styles you can use are as follows:

Character Description
‘-‘ Solid line
‘—‘ Dashed line
‘-.’ Dash-dot line
‘:’ Dotted line
‘H’ Hexagon marker

Here is the syntax:

plt.plot(X, Y, ‘CLM’)

Parameters:

X: This parameter denotes the x-axis.

Y: This parameter denotes the y-axis

CLM: stands for color, line, and Marker.

Let us cover an example for the above-explained function:

import matplotlib.pyplot as plt 
import numpy as np 

X = np.linspace(-np.pi, np.pi, 15) 
C = np.cos(X) 
S = np.sin(X) 

# [left, bottom, width, height] 
ax = plt.axes([0.1, 0.1, 0.8, 0.8]) 

ax1 = ax.plot(X, C, 'mx:') 

ax2 = ax.plot(X, S, 'cD-') 

ax.legend(labels = ('Cosine Function', 'Sine Function'),loc = 'upper left') 

ax.set_title("Trigonometric Functions in Mathematics") 

plt.show() 

The output for the above code snippet is as shown below:

Matplotlib Axes class and axes function

Summary:

In this tutorial we covered the matplotlib Axes class and how to use it with code examples and outputs. We also covered various different functions of this class and how they can be used.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.