matplotlib - 高斯曲线拟合:我的曲线不适合我的直方图
问题描述
首先,我导入包并从我的 30 个数据集中生成两个数组“t”和“电压”。
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) 不管我输入什么值,还是只能得到一条竖线。
或者问题是我的曲线有多个峰值?
解决方案
推荐阅读
- c# - 调用正确方法的通用对象
- business-intelligence - 在比较业务对象 webi 中的两个不同日期后找到最大结果
- r - 如何使用 dplyr::arrange 首先对 NA 进行排序?
- r - 如何在某些条件下选择矩阵的元素?
- angularjs - 重写规则弄乱了 web api 2 /token
- pine-script - 根据满足条件的数量创建整体评级或数量?更高数量的标准创建绿色渐变
- c - Linux堆栈的动态扩展
- wordpress - 无法使用基本的“如果有标签”语句 - Wordpress
- java - 使用 Java,从 Spark 数据帧中删除空白记录(带有 null 和所有类型的空白)
- jhipster - 为什么我的 jdl 文件生成的域有代码错误?