python将折线平滑为曲线

曲线的曲率介绍

曲线的曲率(curvature):就是针对曲线上某个点的切线方向角对弧长的转动率,通过微分来定义,表明曲线偏离直线的程度。数学上表明曲线在某一点的弯曲程度的数值。

曲率越大,表示曲线的弯曲程度越大。曲率的倒数就是曲率半径。

  • 曲线的两端的端点是没有曲率可谈的,和滑动平均类似,曲率的求解方式和滑动平均有类似之处,
  • 曲率达到一定范围,才认为其有拐点

平滑方法介绍

1. 环境及模块介绍:

  1. 环境
  • 编译器:python 3.x
  1. 需要使用的模块:
  • 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

2. 代码示例

  1. 随机生成10个坐标点
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import make_interp_spline
import matplotlib

matplotlib.use('TkAgg')

x = np.linspace(0, 20, 10)
y = np.random.uniform(-10, 10, 10)
  1. 先画出散点图看看
plt.figure(figsize=(10, 5), dpi=100)
# plt.plot(x, y)
plt.scatter(x, y, c="red")
plt.grid(True)
plt.show()

在这里插入图片描述
3. 折线图

plt.figure(figsize=(10, 5), dpi=100)
plt.plot(x, y)
plt.scatter(x, y, c="red")
plt.grid(True)
plt.show()

在这里插入图片描述
4. 平滑曲线

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()

在这里插入图片描述

  1. 对比(折线与平滑曲线)
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()

在这里插入图片描述

3. 整体代码

# -*- coding:utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import make_interp_spline
import matplotlib

matplotlib.use('TkAgg')

x = np.linspace(0, 20, 10)
y = np.random.uniform(-10, 10, 10)

plt.figure(figsize=(10, 5), dpi=100)
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()

print('over...')
  • 9
    点赞
  • 57
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值