English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Matplotlib Axes Class

An Axes object is an image area with a data space. A given figure can contain many axes, but a given Axes object can only be in one figure. Axes contain two (or3Three Axis objects in case D. The Axes class and its member functions are the main entry points for the OO interface.

By calling the add_axes() method, an Axes object is added to the figure. It returns the axis object and adds an axis at the position rect [left, bottom, width, height], where all quantities are fractions of the width and height of the figure.

Parameter

The following are the parameters of the Axes class -

rect - 4a sequence of [left, bottom, width, height] quantities.

 # Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
ax = fig.add_axes([0,0,1,1])
The following member functions of the axis class add different elements to the figure -

Example

The legend() method of the axes class adds a legend to the plotting graph. It requires three parameters. -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
ax.legend(handles, labels, loc)

where label is a series of strings, processing a series of Line2D or Patch instance. loc can be a string or integer specifying the legend position.

Position stringPosition code
best0
upper right1
upper left2
lower left3
lower right4
right5
center left6
center right7
lower center8
upper center9
center10

axes.plot()

This is a basic method of the axis class, which draws the values of one array against another array as a line or marker. The plot() method can have an optional format string parameter to specify the color, style, and size of the line and marker.

Color code

Character markerColor
bBlue
gGreen
rRed
bBlue
cCyan
mMagenta
yYellow
kBlack
wWhite

Marker code

Character markerDescription
.Dot marker
oCircular marker
xX marker
DDiamond marker
HHexagonal marker
sSquare marker
+Plus sign marker

Line style

CharacterDescription
-Solid line
--Dashed line
-.Single dash line
:Dashed line
HHexagonal marker

The following example displays the advertising expenses and sales figures of television and smartphones in the form of a line chart. The line representing television is a solid line with yellow and square markers, while the smartphone line is a dashed line with green and circular markers.

Reference Implementation Code -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : www.oldtoolbag.com
# Date : 2020-08-08
#! /usr/bin/env python
 #coding=utf-8
 import matplotlib.pyplot as plt
 # Display Chinese settings...
 plt.rcParams['font.sans-serif'] = ['SimHei']  # Step one (replace sans-serif font)
 plt.rcParams['axes.unicode_minus'] = False  # Step two (solve the problem of displaying negative signs on the axis)
 y = [1, 4, 9, 16, 25,36,49, 64]
 x1 = [1, 16, 30, 42,55, 68, 77,88]
 x2 = [1,6,12,18,28, 40, 52, 65]
 fig = plt.figure()
 ax = fig.add_axes([0,0,1,1])
 l1 = ax.plot(x1,y,'ys-')  # solid line with yellow colour and square marker
 l2 = ax.plot(x2,y,'go--')  # dash line with green colour and circle marker
 ax.legend(labels=('Television', 'Smartphone'), loc='upper left')  # legend placed at lower right
 ax.set_title("The Impact of Advertising on Sales")
 ax.set_xlabel('Medium')
 ax.set_ylabel('Sales')
 plt.show()

Execute the above example code to get the following results -