English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.
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 -
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 string | Position 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 |
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.
Character marker | Color |
b | Blue |
g | Green |
r | Red |
b | Blue |
c | Cyan |
m | Magenta |
y | Yellow |
k | Black |
w | White |
Character marker | Description |
. | Dot marker |
o | Circular marker |
x | X marker |
D | Diamond marker |
H | Hexagonal marker |
s | Square marker |
+ | Plus sign marker |
Character | Description |
- | Solid line |
-- | Dashed line |
-. | Single dash line |
: | Dashed line |
H | Hexagonal 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 -