python Matplotlib 画图教程详解

发表于   |   更新于
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 首先导入模块
import matplotlib.pyplot as plt
import numpy as np

# 定义x范围
x = np.linspace(-2, 3, 50)
y1 = x**2
y2 = 2*x
plt.figure()
l1, = plt.plot(x,y1,label = 'square line')
l2, = plt.plot(x, y2, color='red', linewidth=1.0, linestyle='--',label='linear line')

# 设置坐标轴
plt.xlim((-1, 2))
plt.ylim((-2, 3))

# 获取当前坐标轴信息
ax = plt.gca()

# 将右侧和上方边框设为白色
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')

# 设置底部边框(x轴)的位置y=0
ax.spines['bottom'].set_position(('data', 0))

# 设置左侧边框(y轴)的位置x=0
ax.spines['left'].set_position(('data',0))

# 修改图例
plt.legend(handles=[l1, l2], labels=['hmm', 'wtf'], loc='best')
plt.show()

# 多图
# plt.subplot(2,2,1)

# dynamic figure
# plt.figure()
# plt.ion()
# plt.cla()
# plt.pause(0.5)
# plt.ioff()
# plt.show()

Comments: