M
a
t
p
l
o
t
l
i
b
Matplotlib
Matplotlib 是一个
P
y
t
h
o
n
Python
Python 的
2
D
2D
2D 绘图库,与
N
u
m
P
y
NumPy
NumPy 联合使用。
文章目录
一、Pyplot
P y p l o t Pyplot Pyplot 是 M a t p l o t l i b Matplotlib Matplotlib 的子库,提供了和 M A T L A B MATLAB MATLAB 类似的绘图 A P I API API。 P y p l o t Pyplot Pyplot 是常用的绘图模块,能很方便让用户绘制 2 D 2D 2D 图表。 P y p l o t Pyplot Pyplot 包含一系列绘图函数的相关函数,每个函数会对当前的图像进行一些修改,例如:给图像加上标记,生新的图像,在图像中产生新的绘图区域等等。
但是一般来说, P y p l o t Pyplot Pyplot 六个字母也太长了,所以我们一般用 p l t plt plt 三个字母来代替。
import matplotlib.pyplot as plt
1. 折线图
p l t . p l o t plt.plot plt.plot可以实现画点和画线的功能。这个函数的基本使用方法如下:
# 画单条线
plot([x], y, [fmt], *, data=None, **kwargs)
# 画多条线
plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)
#画点 (就是加入'o'参数)
plt.plot(xpoints, ypoints, 'o')
对参数进行解释:
x
,
y
x, y
x,y:点或线的节点,
x
x
x 为
x
x
x 轴数据,
y
y
y 为
y
y
y 轴数据,数据可以列表或数组。注意这里
x
x
x是可选的参数,如果不写的话那就是默认是
[
0
,
1
,
2
,
.
.
.
,
n
−
1
]
[0, 1, 2, ..., n-1]
[0,1,2,...,n−1]
f
m
t
fmt
fmt:可选,定义基本格式(如颜色、标记和线条样式)。
∗
∗
k
w
a
r
g
s
**kwargs
∗∗kwargs:可选,用在二维平面图上,设置指定属性,如标签,线的宽度等。
一些 f m t fmt fmt 的举例:
颜色字符:‘
b
b
b’ 蓝色,‘
m
m
m’ 洋红色,‘
g
g
g’ 绿色,‘
y
y
y’ 黄色,‘
r
r
r’ 红色,‘
k
k
k’ 黑色,‘
w
w
w’ 白色,‘
c
c
c’ 青绿色,‘
#
008000
\#008000
#008000’
R
G
B
RGB
RGB 颜色符串。多条曲线不指定颜色时,会自动选择不同颜色。
线型参数:‘‐’ 实线,‘‐‐’ 破折线,‘‐.’ 点划线,‘:’ 虚线。
标记字符:‘.’ 点标记,‘,’ 像素标记(极小点),‘o’ 实心圈标记,‘v’ 倒三角标记,‘^’ 上三角标记,‘>’ 右三角标记,‘<’ 左三角标记…等等。
1.1 单条线
1.1.1 直线
比如我们要画一条从 [ 1 , 1 ] [1, 1] [1,1]到 [ 5 , 10 ] [5, 10] [5,10]的线,那我们就
import numpy as np
import matplotlib.pyplot as plt
xpoints = np.array([1, 5])
ypoints = np.array([1, 10])
plt.plot(xpoints, ypoints)
plt.show()
注意:
p
l
t
.
s
h
o
w
(
)
plt.show()
plt.show()的意思就是把这形状显示出来。结果如下图所示:
1.1.2 折线
就是说我们可以输入多个点,这样显示出来就会是折线。举个例子:
import numpy as np
import matplotlib.pyplot as plt
xpoints = np.array([1, 5, 7, 9])
ypoints = np.array([1, 10, 5, 20])
plt.plot(xpoints, ypoints)
plt.show()
这就是从
[
1
,
1
]
[1,1]
[1,1]到
[
5
,
10
]
[5,10]
[5,10]到
[
7
,
5
]
[7,5]
[7,5]再到
[
9
,
20
]
[9,20]
[9,20]的一条折线。显示如下:
1.1.3 曲线
整一条正弦函数。
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 4 * np.pi, 0.1)
y = np.sin(x)
plt.plot(x, y)
plt.show()
输出如图:
1.2 多条线
就是 p l t . p l o t ( ) plt.plot() plt.plot()函数里面可以有多条线。比如说我要输出正弦和余弦曲线:
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(0, 4 * np.pi, 0.1)
y = np.sin(x)
z = np.cos(x)
plt.plot(x, y, x, z)
plt.show()
图像就会是:
1.3 点
就是加 ′ o ′ 'o' ′o′参数就可以了。用1.1.1里面的直线举例来说的话:
import numpy as np
import matplotlib.pyplot as plt
xpoints = np.array([1, 5])
ypoints = np.array([1, 10])
plt.plot(xpoints, ypoints, 'o')
plt.show()
要注意:如果有多条线的话,那么在
p
l
o
t
plot
plot里面的每一对
(
x
,
y
)
(x,y)
(x,y)的后面都要加
′
o
′
'o'
′o′
1.4 修饰
1.4.1 绘图标记
就是用 p l o t ( ) plot() plot()函数的 m a r k e r marker marker参数来进行定义。用上面的折线图来距离的话,只需要在 p l o t plot plot里面加 m a r k e r = ′ o ′ marker = 'o' marker=′o′即可。
import numpy as np
import matplotlib.pyplot as plt
xpoints = np.array([1, 5, 7, 9])
ypoints = np.array([1, 10, 5, 20])
plt.plot(xpoints, ypoints, marker = 'o')
plt.show()
1.4.2 标记形状
标记 | 描述 |
---|---|
“.” | 点 |
“,” | 像素点 |
“o” | 实心圆 |
“v” | 下三角 |
“^” | 上三角 |
“<” | 左三角 |
“>” | 右三角 |
“1” | 下三叉 |
“2” | 上三叉 |
“3” | 左三叉 |
“4” | 右三叉 |
“8” | 八角形 |
“s” | 正方形 |
“p” | 五边形 |
“P” | 加号(填充) |
“*” | 星号 |
“h” | 六边形 1 |
“H” | 六边形 2 |
“+” | 加号 |
“x” | 乘号 x |
“X” | 乘号 x (填充) |
“D” | 菱形 |
“d” | 瘦菱形 |
" | " |
“_” | 横线 |
0 (TICKLEFT) | 左横线 |
1 (TICKRIGHT) | 右横线 |
2 (TICKUP) | 上竖线 |
3 (TICKDOWN) | 下竖线 |
4 (CARETLEFT) | 左箭头 |
5 (CARETRIGHT) | 右箭头 |
6 (CARETUP) | 上箭头 |
7 (CARETDOWN) | 下箭头 |
8 (CARETLEFTBASE) | 左箭头 (中间点为基准) |
9 (CARETRIGHTBASE) | 右箭头 (中间点为基准) |
10 (CARETUPBASE) | 上箭头 (中间点为基准) |
11 (CARETDOWNBASE) | 下箭头 (中间点为基准) |
“None”, " " or “” | 没有任何标记 |
‘ . . . ... ...’ | 渲染指定的字符。例如 “ f f f” 以字母 f 为标记。 |
(上面表格摘自菜鸟教程)
1.4.3 线类型
线类型标记 | 描述 |
---|---|
‘-’ | 实线 |
‘:’ | 虚线 |
‘–’ | 破折线 |
‘-.’ | 点划线 |
(上面表格摘自菜鸟教程)
举例来说就是:
import numpy as np
import matplotlib.pyplot as plt
xpoints = np.array([1, 5, 7, 9])
ypoints = np.array([1, 10, 5, 20])
plt.plot(xpoints, ypoints, '*:')
# 但我更推荐下面的方法 更加直观
# plt.plot(xpoints, ypoints, marker = '*', ls = ':')
# ls是linestyle的简写
plt.show()
注意:与线一起发生样式变化的话"
m
a
r
k
e
r
marker
marker"要省略的。结果就会是:
1.4.4 颜色
颜色标记 | 描述 |
---|---|
‘r’ | 红色 |
‘g’ | 绿色 |
‘b’ | 蓝色 |
‘c’ | 青色 |
‘m’ | 品红 |
‘y’ | 黄色 |
‘k’ | 黑色 |
‘w’ | 白色 |
(上面表格来自菜鸟教程)
举例来说,那就可以是:
import numpy as np
import matplotlib.pyplot as plt
xpoints = np.array([1, 5, 7, 9])
ypoints = np.array([1, 10, 5, 20])
plt.plot(xpoints, ypoints, '*:r')
# 但我更推荐下面的方法 更加直观
# plt.plot(xpoints, ypoints, marker = '*', ls = ':', c = 'r')
# c是color的简写
plt.show()
这样得出来的结果就是:
1.4.5 线宽度
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([6, 2, 13, 10])
plt.plot(ypoints, linewidth = '12.5')
plt.show()
效果如下:
1.4.6 对标记进行修改
有的同学就急了哈,怎么上面的修改都是针对线的啊?那标记点呢那标记点呢?
标记点的样式是这么修改的哈!
m
a
r
k
e
r
s
i
z
e
markersize
markersize,简写为
m
s
ms
ms:定义标记的大小。
m
a
r
k
e
r
f
a
c
e
c
o
l
o
r
markerfacecolor
markerfacecolor,简写为
m
f
c
mfc
mfc:定义标记内部的颜色。
m
a
r
k
e
r
e
d
g
e
c
o
l
o
r
markeredgecolor
markeredgecolor,简写为
m
e
c
mec
mec:定义标记边框的颜色。
简单举个例子:
import numpy as np
import matplotlib.pyplot as plt
xpoints = np.array([1, 5, 7, 9])
ypoints = np.array([1, 10, 5, 20])
plt.plot(xpoints, ypoints, '*:r', ms = 40)
plt.show()
这样做的结果就会是:
2.图表的修改
2.1 轴标签
我们可以分别给 x x x 轴和 y y y 轴设定标签。基本的设定方法是:
import numpy as np
import matplotlib.pyplot as plt
plt.xlabel("x - label name")
plt.ylabel("y - label name")
我们继续以 1.1.2 1.1.2 1.1.2 的折线为例:
import numpy as np
import matplotlib.pyplot as plt
xpoints = np.array([1, 5, 7, 9])
ypoints = np.array([1, 10, 5, 20])
plt.xlabel("xpoints")
plt.ylabel("ypoints")
plt.plot(xpoints, ypoints)
plt.show()
结果如下图所示:
这个函数提供了
l
o
c
loc
loc 参数来设置
x
x
x 轴显示的位置,可以设置为: ‘
l
e
f
t
left
left’, ‘
r
i
g
h
t
right
right’, 和 ‘
c
e
n
t
e
r
center
center’, 默认值为 ‘
c
e
n
t
e
r
center
center’。设置
x
x
x 轴显示的位置,则可以设置为: ‘
t
o
p
top
top’, ‘
b
o
t
t
o
m
bottom
bottom’, 和 ‘
c
e
n
t
e
r
center
center’, 默认值为 ‘
c
e
n
t
e
r
center
center’。
也可以用 s i z e size size 参数来设置大小。示例:
plt.xlabel("xpoints", size = '20')
例如:
import numpy as np
import matplotlib.pyplot as plt
xpoints = np.array([1, 5, 7, 9])
ypoints = np.array([1, 10, 5, 20])
plt.xlabel("xpoints", loc = 'left')
plt.ylabel("ypoints", loc = 'top')
plt.plot(xpoints, ypoints)
plt.show()
结果如图:
2.2 标题
import numpy as np
import matplotlib.pyplot as plt
xpoints = np.array([1, 5, 7, 9])
ypoints = np.array([1, 10, 5, 20])
plt.xlabel("xpoints", loc = 'left')
plt.ylabel("ypoints", loc = 'top')
plt.title("TITLE")
plt.plot(xpoints, ypoints)
plt.show()
结果如下图所示:
2.3 网格线
plt.grid(axis = 'x', color = 'r', linestyle = '--', linewidth = 0.5)
嫌麻烦就列举了常用的参数:
a
x
i
s
axis
axis :显示哪个方向的网格线。默认为’
b
o
t
h
both
both '。
c
o
l
o
r
color
color:网格线颜色
l
i
n
e
s
t
y
l
e
linestyle
linestyle:网格线样式
l
i
n
e
w
i
d
t
h
linewidth
linewidth:网格线宽度
上面三个就参考前面的样式就可以了。
import numpy as np
import matplotlib.pyplot as plt
xpoints = np.array([1, 5, 7, 9])
ypoints = np.array([1, 10, 5, 20])
plt.xlabel("xpoints", loc = 'left')
plt.ylabel("ypoints", loc = 'top')
plt.title("TITLE")
plt.grid(axis = 'x', color = 'r', linestyle = '--', linewidth = 0.5)
plt.plot(xpoints, ypoints)
plt.show()
结果如下图所示:
3.柱形图
可以利用 p l t . b a r ( ) plt.bar() plt.bar() 函数来实现:
import matplotlib.pyplot as plt
plt.bar(x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs)
参数说明:
- x x x:柱形图的 x x x 轴数据。
- h e i g h t height height:柱形图的高度。
- w i d t h width width:柱形图的宽度。
- b o t t o m bottom bottom:底座的 y y y 坐标,默认 0 0 0。
- a l i g n align align:柱形图与 x x x 坐标的对齐方式,‘ c e n t e r center center’ 以 x x x 位置为中心,这是默认值。 ‘ e d g e edge edge’:将柱形图的左边缘与 x x x 位置对齐。要对齐右边缘的条形,可以传递负数的宽度值及 a l i g n = ′ e d g e ′ align='edge' align=′edge′。
- ∗ ∗ k w a r g s **kwargs ∗∗kwargs:其他参数。
举例说明:
import matplotlib.pyplot as plt
import numpy as np
x = np.array(["x1", "x2", "x3", "x4"])
y = np.array([10, 12, 16, 28])
plt.bar(x, y)
plt.show()
效果如图所示:
如果想要横着的,那就把 b a r bar bar 换成 b a r h barh barh。
import matplotlib.pyplot as plt
import numpy as np
x = np.array(["x1", "x2", "x3", "x4"])
y = np.array([10, 12, 16, 28])
plt.barh(x, y)
plt.show()
修改样式:
import matplotlib.pyplot as plt
import numpy as np
x = np.array(["x1", "x2", "x3", "x4"])
y = np.array([10, 12, 16, 28])
plt.bar(x, y, color = ["#4CAF50","red","hotpink","#556B2F"], width = 0.1)
plt.show()
参数说明:
- c o l o r color color:颜色。如果要统一的颜色,就直接 c o l o r = color = color= 就可以了。如果要设计每一个的颜色就要像样例代码一样设置了。
- w i d t h width width:柱形图的宽度。 b a r h barh barh 函数所对应的应该是 h e i g h t height height。
4.饼图
真的懒了,不想写了。
import matplotlib.pyplot as plt
import numpy as np
y = np.array([30, 30, 25, 15])
plt.pie(y,
labels = ['A', 'B', 'C', 'D'],
colors = ["#d5695d", "#5d8ca8", "#65a479", "#a564c9"],
explode = (0, 0.2, 0, 0),
autopct = '%.2f%%',
)
plt.show()
参数解释:
- l a b e l s labels labels:设置饼图标签
- c o l o r s colors colors:设置饼图颜色
- e x p l o d e explode explode:突出显示,值越大,距离中心越远
- a u t o p c d autopcd autopcd:格式化输出百分比
二、其他子库
感觉这辈子用不到,等下辈子再更新这部分吧。