Article directory
Show Chinese and minus sign
- matplotlib uses the English font library by default. Chinese characters will be garbled. You need to specify the Chinese font library.
matplotlib.rcParams['font.family']='simHei' #黑体
matplotlib.pyplot.rcParams['axes.unicode_minus'] = False # 显示负号
1. Various drawing functions
1.1 matplotlib.pyplot.plot
def plot(*args, scalex=True, scaley=True, data=None, **kwargs):...
Common parameters:
parameter | meaning |
---|---|
first parameter | Abscissa array (list, numpy array, pandas spreadsheet) |
second parameter | Vertical coordinate array (list, numpy array, pandas spreadsheet) |
label | legend |
color | color |
linestyle | linear |
marker | Attached point style |
Parameter Description:
- The array lengths of the first parameter and the second parameter must be the same, one-to-one correspondence
- Legends, colors, etc. will be introduced in detail in Section 2
1.2 matplotlib.pyplot.scatter
def scatter(
x, y, s=None, c=None, marker=None, cmap=None, norm=None,
vmin=None, vmax=None, alpha=None, linewidths=None, *,
edgecolors=None, plotnonfinite=False, data=None, **kwargs):...
Common parameters:
parameter | meaning |
---|---|
x | Abscissa array (list, numpy array, pandas spreadsheet) |
y | Vertical coordinate array (list, numpy array, pandas spreadsheet) |
s | dot size |
marker | point style |
label | legend |
color | color |
Parameter Description:
- The array lengths of the x parameter and the y parameter must be the same and correspond one to one.
- s represents the size of the point, the default is 20, it can also be an array, each parameter of the array is the size of the corresponding point
- Legends, colors, etc. will be introduced in detail in Section 2
1.3 matplotlib.pyplot.bar
def bar(
x, height, width=0.8, bottom=None, *, align='center',
data=None, **kwargs):...
Common parameters:
parameter | meaning |
---|---|
x | Array, the x-axis data of the column chart |
height | Array, the y-axis data of the column chart |
width | Column chart width |
bottom | The offset of height coordinates, the default is 0 |
align | The alignment of the histogram to the x-coordinate |
color | color |
label | legend |
tick_label | A string or array of strings to use to replace the x-axis data |
Parameter Description:
- The array lengths of the x parameter and the height parameter must be the same and correspond one to one.
- width represents the width of the column, or it can be an array, corresponding to the horizontal width of each column
- bottom represents the offset of the height coordinate, or it can be an array, corresponding to the y offset of each column.
- align optional parameters: center alignment "center", left alignment "edge"
- tick_label can replace the number on the x-axis, or it can be an array, corresponding to the x-axis position of each bar
- Legends, colors, etc. will be introduced in detail in Section 2
Here is an example of drawing two histograms on one graph:
import numpy as np
import matplotlib.pyplot as plt
# 数据
x = np.arange(4)
Bj = [52, 55, 63, 53]
Sh = [44, 66, 55, 41]
bar_width = 0.3
# 绘图 x 表示 从那里开始
plt.bar(x, Bj, bar_width)
plt.bar(x+bar_width, Sh, bar_width, align="center")
# 展示图片
plt.show()
1.4 matplotlib.pyplot.pie
def pie(
x, explode=None, labels=None, colors=None, autopct=None,
pctdistance=0.6, shadow=False, labeldistance=1.1,
startangle=0, radius=1, counterclock=True, wedgeprops=None,
textprops=None, center=(0, 0), frame=False,
rotatelabels=False, *, normalize=True, hatch=None, data=None):
Common parameters:
parameter | meaning |
---|---|
x | Array, data used to draw pie charts, representing the area of each sector |
explode | Array representing the intervals between sectors |
labels | List, labels for each sector |
colors | Array representing the color of each sector |
autopct | String, set the display format of each sector percentage in the pie chart |
radius | Set the radius of the pie chart |
Parameter Description:
- The array lengths of the x parameter, labels parameter, and colors parameter must be the same and correspond one to one.
- explode determines how the graphics are displayed
- Autopct example: %d%% integer percentage, %0.1f one decimal place, %0.1f%% one decimal percentage, %0.2f%% two decimal points
1.5 matplotlib.pyplot.hist
def hist(
x, bins=None, range=None, density=False, weights=None,
cumulative=False, bottom=None, histtype='bar', align='mid',
orientation='vertical', rwidth=None, log=False, color=None,
label=None, stacked=False, *, data=None, **kwargs):...
Common parameters:
parameter | meaning |
---|---|
x | An array representing the data to draw a histogram |
bins | Number of bins for the histogram |
colors | color |
label | legend |
Parameter Description:
- Bins example: If the bins parameter is 30, this means that the data range is divided into 30 equal-width intervals, and then the frequency of data in each interval is counted.
- Legends, colors, etc. will be introduced in detail in Section 2
2. Drawing style
2.1 Axis labels and titles
Axis labels (matplotlib.pyplot.xlabel and matplotlib.pyplot.ylabel)
def xlabel(xlabel, fontdict=None, labelpad=None, *, loc=None, **kwargs):...
def ylabel(ylabel, fontdict=None, labelpad=None, *, loc=None, **kwargs):...
- The loc parameter is only
"left"
,"center"
,"right "
optional
title
def title(label, fontdict=None, loc=None, pad=None, *, y=None, **kwargs):
- The loc parameter is only
"left"
,"center"
,"right "
optional
2.2 Legend position (matplotlib.pyplot.legend)
- After specifying the label legend when drawing, if you do not use the matplotlib.pyplot.legend function to specify the legend position, the legend will not be displayed.
def legend(*args, **kwargs):...
- The loc parameters are as follows:
Optional parameters |
---|
'upper right' |
'upper left' |
'lower left' |
'lower right' |
'right' |
'center left' |
'center right' |
'lower center' |
'upper center' |
'center' |
2.3 Optional color (color)
Commonly used colors are as follows:
color marking | describe |
---|---|
'r' |
red |
'g' |
green |
'b' |
blue |
'c' |
blue |
'm' |
magenta |
'y' |
yellow |
'k' |
black |
'w' |
White |
2.4 line style (linestyle)
Commonly used line types are as follows:
Line mark | describe |
---|---|
'-' |
solid line |
':' |
dotted line |
'--' |
dashed line |
'-.' |
Dotted line |
2.5 point style (marker)
常用样式如下:
常用颜色如下:
可选markder | 样式 | 描述 |
---|---|---|
"." |
点 | |
"," |
像素点 | |
"o" |
实心圆 | |
"v" |
下三角 | |
"^" |
上三角 | |
"<" |
左三角 | |
">" |
右三角 | |
"1" |
下三叉 | |
"2" |
上三叉 | |
"3" |
左三叉 | |
"4" |
右三叉 | |
"8" |
八角形 | |
"s" |
正方形 | |
"p" |
五边形 | |
"P" |
填充的加号 | |
"*" |
星号 | |
"h" |
六边形1 | |
"H" |
六边形2 | |
"+" |
加号 | |
"x" |
乘号 | |
"X" |
填充的乘号 | |
"D" |
菱形 | |
"d" |
瘦菱形 | |
"|" |
竖线 | |
"_" |
横线 | |
4 |
左箭头 | |
5 |
右箭头 | |
6 |
上箭头 | |
7 |
下箭头 |
3.画布管理与多图
3.1 创建一张画布上的多图
def subplots(nrows=1, ncols=1, *, sharex=False, sharey=False, squeeze=True,
width_ratios=None, height_ratios=None,
subplot_kw=None, gridspec_kw=None, **fig_kw):...
参数说明:
- nrows表示子图的行数,ncols表示子图的列数
返回值说明:
- 返回一个有2个元素的元组,分别为fig和ax。
- fig为这张画布
- ax为子图列表,想在第一张图上画折线图:调用ax[0].plot
3.2 清理
- 清理一张图上所有内容:
plt.cla()
- 清理一张画布上的所有图:
plt.clf()
动态图
- 先开启交互模式,然后提前展示画布
plt.ion()
plt.show()
- 在每轮绘制前先进行清理,然后停留
plt.cla()
plt.plot(...)
plt.pause(0.001)