使用python代码2.7版进行样条拟合(从excel文件中读取x和y的值)

时间:2019-01-03 17:33:45

标签: python scipy

我想简单地从Excel文件(sheet1)中以X和Y的形式读入两列数据(值),并对数据进行样条曲线拟合。这样我就可以选择x的任何期望值(假设为2.6)并计算y。

您能指导我吗?

我在下面尝试了两种方法,它们都不起作用!

我尝试这样做的两种方法:

第一种方法:

from scipy import interpolate
import numpy as np
from openpyxl import load_workbook


#read  from excel file

wb = load_workbook('python_excel_read.xlsx')

sheet1 = wb.get_sheet_by_name('Sheet1')


x = np.zeros(sheet1.max_row)
y = np.zeros(sheet1.max_row)

for i in range(0,sheet1.max_row):
    x[i]=sheet1.cell(row=i+1, column=1).value
    y[i]=sheet1.cell(row=i+1, column=2).value



def f(x):
    x_current = [x]
    y_voltage = [y]


    tck = interpolate.splrep(x_current, y_voltage)
    return interpolate.splev(x, tck)

print f(2.6)

第二种方法:

from scipy import interpolate

import xlwings as xw

wb = xw.Book('python_excel_read.xlsx')
sht = xw.Book('Sheet1')

x_current = sht.range1('A2:A40').value
y_voltage = sht.range2('B2:B40').value


def f(x):
    x_current = [sht.range1]
    y_voltage = [sht.range2]


    tck = interpolate.splrep(x_current, y_voltage)
    return interpolate.splev(x, tck)

print f(2.6)

0 个答案:

没有答案