Python三次样条插值

本程序实现从txt文件中读取两列数据,然后进行三次样条插值,绘制出一条平滑的曲线。
需要解决的一些问题:

方法一:使用 np.loadtxt( ) 方法读取数据

# code:utf-8  	Ubuntu
import matplotlib.pyplot as plt
from scipy import interpolate
import numpy as np

import matplotlib.font_manager as mpt
zhfont=mpt.FontProperties(fname='/usr/share/fonts/custom/msyh.ttf') #显示中文字体
#导入数据
file = 'data.txt'
a = np.loadtxt(file)
# 数组切片
x = a[:,0]  # 取第一列数据
y = a[:,1]  # 取第二列数据
# 进行样条插值
tck = interpolate.splrep(x,y)
xx = np.linspace(min(x),max(x),100)
yy = interpolate.splev(xx,tck,der=0)
print(xx)
# 画图
plt.plot(x,y,'o',xx,yy)
plt.legend(['true','Cubic-Spline'])
plt.xlabel('距离(cm)', fontproperties=zhfont) #注意后面的字体属性
plt.ylabel('%')
plt.title('管线仪实测剖面图', fontproperties=zhfont)  
# 保存图片  
plt.savefig('out.jpg')
plt.show()

绘图结果
方法2:使用 Pandas 读取数据

# code:utf-8  	Windows 7 Utilmate
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from scipy import interpolate

# 解决中文显示问题
import matplotlib as mpl
mpl.rcParams["font.sans-serif"] = ["SimHei"]
mpl.rcParams["axes.unicode_minus"] = False

# 导入数据
file = pd.read_csv('data.txt',sep='\s+',
                header = None, skiprows=[17],
                # skiprows 跳过第18行
                names = ['x', 'value'])
                
data = pd.DataFrame(file)
# 数组切片
x = data['x']  # 取第一列数据
y = data['value']  # 取第二列数据
# 进行样条插值
tck = interpolate.splrep(x,y)
xx = np.linspace(min(x),max(x),100)
yy = interpolate.splev(xx,tck,der=0)
print(yy)
# 画图
plt.plot(x,y,'o',xx,yy)
plt.legend(['true','Cubic-Spline'])
plt.xlabel('距离(cm)')
plt.ylabel('%')
plt.title('管线仪实测剖面图')  
# 保存图片  
plt.savefig('out2.png',dpi=600 )
# 设置需要保存图片的分辨率
plt.show()

在这里插入图片描述
txt 文本内容

0	93
30	96
60	84
90	84
120	48
150	38
180	51
210	57
240	40
270	45
300	50
330	75
360	80
390	60
420	72
450	67
480	71
510	7
540	74
570	63
600	69
三次样条插值是一种常用的插值方法,可以通过已知数据点的坐标来估计在其他位置的函数值。在Python中,可以使用SciPy库中的interpolate模块来进行三次样条插值。 方法一是使用np.loadtxt()方法读取数据,然后使用interpolate.splrep()函数进行样条插值。首先,将数据加载到数组中,然后使用数组切片获取x和y的值。接下来,使用splrep()函数计算样条插值的参数tck。然后,使用np.linspace()函数生成一系列等间距的x值,再使用splev()函数根据tck参数计算对应的y值。最后,使用plt.plot()函数绘制原始数据点和插值曲线,并使用plt.legend()、plt.xlabel()、plt.ylabel()和plt.title()函数设置图例、坐标轴标签和标题。最后,使用plt.savefig()函数保存图片,使用plt.show()函数显示图片。\[2\] 方法二是使用Pandas库的read_csv()函数读取数据,然后使用interpolate.splrep()函数进行样条插值。首先,使用read_csv()函数读取数据文件,并使用skiprows参数跳过指定行数。然后,使用DataFrame()函数将读取的数据转换为DataFrame对象。接下来,使用数组切片获取x和y的值。然后,使用splrep()函数计算样条插值的参数tck。然后,使用np.linspace()函数生成一系列等间距的x值,再使用splev()函数根据tck参数计算对应的y值。最后,使用plt.plot()函数绘制原始数据点和插值曲线,并使用plt.legend()、plt.xlabel()、plt.ylabel()和plt.title()函数设置图例、坐标轴标签和标题。最后,使用plt.savefig()函数保存图片,使用plt.show()函数显示图片。\[1\] 你可以根据自己的需求选择其中一种方法进行三次样条插值。关于插值方法的更多信息,可以参考SciPy官方文档中的插值方法介绍。\[3\] #### 引用[.reference_title] - *1* *2* *3* [Python三次样条插值](https://blog.csdn.net/qq_41365597/article/details/90676249)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值