使用的是python matplotlib库中的pyplot
先从最基础的来:
(1)绘制连续和离散的函数:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(1, 10, 20)
y1 = x ** 2 + 1
y2 = x
plt.figure()
plt.plot(x, y1) # 画线
plt.scatter(x, y2) # 画点
plt.show()
(2)进阶,在坐标系中画出二维坐标点:
import matplotlib.pyplot as plt
import numpy as np
List = []
i = 0
x1 = np.random.randint(0, 100, 20)
y1 = np.random.randint(0, 100, 20)
num = x1.size
while i < num:
List.append([x1[i], y1[i]])
i = i + 1
List = np.array(List)
plt.figure()
x = List[:, 0]
y = List[:, 1]
plt.scatter(x, y)
plt.show()
(3)为二维点标上序号,形成轨迹
import matplotlib.pyplot as plt
import numpy as np
List = []
i = 0
# 生成随机横纵坐标
x1 = np.random.randint(0, 100, 20)
y1 = np.random.randint(0, 100, 20)
num = x1.size
n = np.arange(num)
while i < num:
List.append([x1[i], y1[i]])
i = i + 1
List = np.array(List)
# 这句不能少
fig, ax = plt.subplots()
ax.plot(List[:, 0], List[:, 1], c='r')
# 为点标上序号
for i, txt in enumerate(n):
ax.annotate(txt, (List[i][0], List[i][1]))
plt.show()