首先,我要说我对使用python进行曲线拟合是完全陌生的,所以我可能在做某些事情,这显然是错误的。

我有一个实验性的“原始”数据集,由温度(x)与信号(y)组成。

我正在尝试使用scipy.curve_fit将玻尔兹曼方程拟合到此数据。我的脚本没有引发任何错误,但是当我在matplotlib中对其进行绘制时,当我的实验数据域仅包含〜308-> 400之间的值时,它将利用0到600之间的x值。不仅如此,它还适合数据>似乎完全消失了(偏移和倾斜),但是它的派生看起来与原始数据的派生相同...让我觉得在某处应用了某种变换。

#Get the data
file = 'Data/my_excel_file.xlsx'
df = pd.read_excel(file, sheet_name='simplified')

#Define the Boltzmann function for fitting
def BoltzmannFitEquation(T, Fmin, Fmax, Tm, breadth):
    return Fmin + ((Fmax - Fmin) / (1 + np.exp((Tm - (T/breadth)))))

#Grabbing the y-values (signal) from the dataframe
signal = df['signal'].tolist()

#Convert my temps from the dataframe from C to K.
for temp in temps_c:
    temps_k.append(float(temp) + 273)

#Now lets fit a Boltzmann equation to the smoothed data
p0 = [0.9, 1.2, 347, 1] #initial predictions
c, cov = curve_fit(BoltzmannFitEquation, temps_k, signal, p0)

yp = BoltzmannFitEquation(temps_k, c[0], c[1], c[2], c[3]) #Plot of the prediction with the optimized coefficients

plt.plot(yp)


我已经排除了一些简化代码的代码-但是让我知道您是否想看到一些特定的东西,它可以帮助解决为什么我看到此消息。

蓝线是“原始”数据和导数,橙色线是拟合曲线和导数。

python - Python和Matplotlib绘制超出域的点,曲线拟合不良-LMLPHP

请注意,拐点在顶部图表上如何不匹配,而在底部图表上却匹配。为什么曲线拟合如此糟糕?为什么还要包含域外的值?

最佳答案

当我尝试以下代码时,我看起来很合适。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

file = 'Data/my_excel_file.xlsx'
#file = '/home/zunzun/temp/temp.xlsx'
df = pd.read_excel(file, sheet_name='simplified')

#Define the Boltzmann function for fitting
def BoltzmannFitEquation(T, Fmin, Fmax, Tm, breadth):
    return Fmin + ((Fmax - Fmin) / (1 + np.exp((Tm - (T/breadth)))))

#Grabbing the data from the dataframe
signal = np.array(df['signal'].tolist())
temps_c = np.array(df['temperature'].tolist())

#Convert my temps from the dataframe from C to K.
temps_k = temps_c + 273.0

#Now lets fit a Boltzmann equation to the smoothed data
p0 = [0.9, 1.2, 347, 1] #initial predictions
c, cov = curve_fit(BoltzmannFitEquation, temps_k, signal, p0)

yp = BoltzmannFitEquation(temps_k, c[0], c[1], c[2], c[3]) #Plot of the prediction with the optimized coefficients

print("Fitted paraneters:", c)

plt.plot(temps_k, signal) # data
plt.plot(temps_k, yp) # fit
plt.show()


python - Python和Matplotlib绘制超出域的点,曲线拟合不良-LMLPHP

关于python - Python和Matplotlib绘制超出域的点,曲线拟合不良,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54428680/

10-12 21:54
查看更多