Python图像处理之直线和曲线的拟合与绘制【curve_fit()应用】
作者:PHILOS_THU 时间:2021-01-28 10:30:49
本文实例讲述了Python图像处理之直线和曲线的拟合与绘制。分享给大家供大家参考,具体如下:
在数据处理和绘图中,我们通常会遇到直线或曲线的拟合问题,python中scipy模块的子模块optimize中提供了一个专门用于曲线拟合的函数curve_fit()
。
下面通过示例来说明一下如何使用curve_fit()
进行直线和曲线的拟合与绘制。
代码如下:
# -*- coding:utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize
#直线方程函数
def f_1(x, A, B):
return A*x + B
#二次曲线方程
def f_2(x, A, B, C):
return A*x*x + B*x + C
#三次曲线方程
def f_3(x, A, B, C, D):
return A*x*x*x + B*x*x + C*x + D
def plot_test():
plt.figure()
#拟合点
x0 = [1, 2, 3, 4, 5]
y0 = [1, 3, 8, 18, 36]
#绘制散点
plt.scatter(x0[:], y0[:], 25, "red")
#直线拟合与绘制
A1, B1 = optimize.curve_fit(f_1, x0, y0)[0]
x1 = np.arange(0, 6, 0.01)
y1 = A1*x1 + B1
plt.plot(x1, y1, "blue")
#二次曲线拟合与绘制
A2, B2, C2 = optimize.curve_fit(f_2, x0, y0)[0]
x2 = np.arange(0, 6, 0.01)
y2 = A2*x2*x2 + B2*x2 + C2
plt.plot(x2, y2, "green")
#三次曲线拟合与绘制
A3, B3, C3, D3= optimize.curve_fit(f_3, x0, y0)[0]
x3 = np.arange(0, 6, 0.01)
y3 = A3*x3*x3*x3 + B3*x3*x3 + C3*x3 + D3
plt.plot(x3, y3, "purple")
plt.title("www.jb51.net test")
plt.xlabel('x')
plt.ylabel('y')
plt.show()
return
plot_test()
拟合和绘制解果如下:
当然,curve_fit()
函数不仅可以用于直线、二次曲线、三次曲线的拟合和绘制,仿照代码中的形式,可以适用于任意形式的曲线的拟合和绘制,只要定义好合适的曲线方程即可。
如高斯曲线拟合,曲线函数形式如下:
def f_gauss(x, A, B, C, sigma):
return A*np.exp(-(x-B)**2/(2*sigma**2)) + C
PS:这里再为大家推荐两款相似的在线工具供大家参考:
在线多项式曲线及曲线函数拟合工具:
http://tools.jb51.net/jisuanqi/create_fun
在线绘制多项式/函数曲线图形工具:
http://tools.jb51.net/jisuanqi/fun_draw
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/guduruyu/article/details/70313176
标签:Python,曲线,拟合
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浏览器是怎样工作的(一):基础知识
2012-05-09 20:32:48
asp sqlserver 执行存储过程返回记录集报对象关闭时不允许操作
2011-03-11 10:57:00
Linux下Resin+JSP+MySQL安装和配置
2009-09-01 10:38:00
SQL Server 作业的备份(备份作业非备份数据库)
2012-07-11 15:58:49
详解MYSQL的备份还原(PHP实现)
2023-10-06 10:44:23
浅析Banner构成与创意设计
2009-11-28 16:25:00
![](https://img.aspxhome.com/file/UploadPic/200911/28/originality-design-55s.jpg)
ASP技巧:Script块不能放在另一个Script 块内
2009-08-19 17:17:00
Python Pandas对缺失值的处理方法
2021-03-18 19:38:55
简单form标准化实例——整体布局
2007-05-11 17:04:00
![](https://img.aspxhome.com/file/uploadpic/20075/200751117421929.gif)
php反序列化之魔术方法超详细讲解
2023-11-16 07:24:59
![](https://img.aspxhome.com/file/2023/1/55621_0s.png)
Python django框架应用中实现获取访问者ip地址示例
2022-01-06 08:08:20
![](https://img.aspxhome.com/file/2023/7/65047_0s.jpg)
根据对象的某一属性进行排序的js代码(如:name,age)
2023-08-31 14:05:43
HTML编辑器FCKeditor使用详解
2010-02-28 12:30:00
![](https://img.aspxhome.com/file/UploadPic/20102/28/fckeditor-87s.gif)
CSS Hacks
2008-07-20 13:04:00
SQL Server 2005返回刚刚插入的数据条目id值
2008-12-04 17:16:00
Thinkphp5.0 框架的请求方式与响应方式分析
2023-11-15 00:07:09
PHP和JS之间的数据交互并处理
2023-05-25 00:57:08
Pandas Matplotlib保存图形时坐标轴标签太长导致显示不全问题的解决
2023-07-22 20:03:09
![](https://img.aspxhome.com/file/2023/0/62940_0s.png)
解决pycharm中导入自己写的.py函数出错问题
2023-07-09 12:12:05
![](https://img.aspxhome.com/file/2023/2/60302_0s.jpg)
PHP count_chars()函数讲解
2023-06-05 09:17:25
![](https://img.aspxhome.com/file/2023/2/55392_0s.png)