首页 > 解决方案 > 高斯曲线拟合:我的曲线不适合我的直方图

问题描述

首先,我导入包并从我的 30 个数据集中生成两个数组“t”和“电压”。

我的 30 个 csv 文件的链接

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from scipy.signal import savgol_filter
plt.style.use(['grid'])

times, voltages = [], []
for i in range(30):
    time, ch1 = np.loadtxt(f"teammate/{i+1}.txt", delimiter=',', skiprows=5,unpack=True)
    times.append(time)
    voltages.append(ch1)

t = (np.array(times[0]) * 1e5)[sliceMin:sliceMax]
voltages = (np.array(voltages))[:, sliceMin:sliceMax]

print(t.shape)
print(voltages.shape)

然后我定义我的高斯并把我所有的关于直方图的细节:

def gauss(x, amp, mu, sigma):
    return amp*np.exp(-(x-mu)**2/(2*sigma**2))

x = np.linspace(0,0.001, 500)

y, bin_edges = np.histogram(voltages*0.001, bins=500, range = (0,0.001), density=True)
y = savgol_filter(y, 51, 3)
bin_centres = (bin_edges[:-1] + bin_edges[1:])/2

在这一点上,我不知道在我的 p0 中放入什么。

popt, pcov = curve_fit(gauss, x,y,  p0=(7,35,5))
prit(popt)

在最后一步,我生成了我的图,但我的曲线不适合直方图。

plt.title("Datapoints Distribution over Voltage [mV]", )      
plt.xlabel("Voltage [mV]")
plt.ylabel("Data Points")
plt.plot(hist)

plt.plot(x,y, color='r', linestyle='dashed')
#plt.xticks(np.arange(min(voltage), max(voltage)+1, 10.0))
plt.show()

是我p0的问题吗?p0的参数(amp, mu, sigma) 不管我输入什么值,还是只能得到一条竖线。

或者问题是我的曲线有多个峰值?

我的图像不适合直方图

我从这个YouTube 视频中得到了曲线拟合

标签: matplotlibcurve-fittingscipy-optimize

解决方案


推荐阅读