环境及使用的模块
编译器:python 3.x
需要使用的模块:
- numpy
- matplotlib
- scipy
# 在终端中输入
pip install numpy -i https://pypi.tsinghua.edu.cn/simple
pip install matplotlib -i https://pypi.tsinghua.edu.cn/simple
pip install scipy -i https://pypi.tsinghua.edu.cn/simple
代码示例
随机生成10个坐标点
x = np.linspace(0, 20, 10)
y = np.random.uniform(-10, 10, 10)
先画出散点图看看
plt.figure(figsize=(10, 5), dpi=100)
# plt.plot(x, y)
plt.scatter(x, y, c="red")
plt.grid(True)
plt.show()
用折线图展示
plt.figure(figsize=(10, 5), dpi=100)
plt.plot(x, y)
plt.scatter(x, y, c="red")
plt.grid(True)
plt.show()
现在将其转换成光滑的曲线
x_smooth = np.linspace(x.min(), x.max(), 400)
y_smooth = make_interp_spline(x, y)(x_smooth)
plt.figure(figsize=(10, 5), dpi=100)
plt.grid(True)
plt.plot(x_smooth, y_smooth)
plt.show()
最后进行一个综合的对比
x_smooth = np.linspace(x.min(), x.max(), 400)
y_smooth = make_interp_spline(x, y)(x_smooth)
plt.figure(figsize=(10, 5), dpi=100)
plt.grid(True)
plt.plot(x_smooth, y_smooth)
plt.plot(x, y)
plt.scatter(x, y, c="red")
plt.show()