Matplotlib - 二维图

二维图

二维图作为常见的图像,熟练掌握是非常必要的. 在此演示如何用 Matplotlib ( python 第三方库 ) 绘制一些常见的二维图.

plot() 可用于绘制 线图散点图.

示例1

效果图

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import numpy as np
import matplotlib.pyplot as plt

fig1= plt.figure()

x = np.arange(0, 6, 0.5)
y = np.arange(6,12,0.5)
y1 = np.arange(5,11,0.5)
y2 = np.arange(4,10,0.5)
y3 = np.arange(3,9,0.5)
y4 = np.arange(2,8,0.5)
y5 = np.arange(1,7,0.5)

plt.plot(x, y,color='#f6416c',linestyle='-',linewidth=3)
plt.plot(x, y1,color='#f38181', linestyle=' ')
plt.plot(x, y2,color='#fce38a', linestyle=(0, (3, 1, 1, 1, 1, 1)))
plt.plot(x, y3,color='#aa96da', linestyle='-.')
plt.plot(x, y4,color='#b83b5e', linestyle=':')
plt.plot(x, y5,color='#95e1d3', linestyle=(0, (3, 10, 1, 10, 1, 10)))

plt.show()

color 线条 颜色
linestyle 线条 样式
linewidth 线条 宽度

示例2

效果图

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import numpy as np
import matplotlib.pyplot as plt

fig1 = plt.figure()

x = np.arange(0, 6, 0.5)
y = np.arange(6, 12, 0.5)
y1 = np.arange(5, 11, 0.5)
y2 = np.arange(4, 10, 0.5)
y3 = np.arange(3, 9, 0.5)
y4 = np.arange(2, 8, 0.5)
y5 = np.arange(1, 7, 0.5)

plt.plot(x, y, color='#f6416c', linestyle=' ', marker='.', markersize=9)
plt.plot(x, y1, color='#f38181', marker='.')
plt.plot(x, y2, color='#fce38a', marker='o')
plt.plot(x, y3, color='#aa96da', marker='^')
plt.plot(x, y4, color='#b83b5e', marker='s')
plt.plot(x, y5, color='#95e1d3', marker='d')

plt.show()

注意第一条线变成了散点.

marker 线条 标记
markersize 线条 标记尺寸