解决方法:IndexError:只有整数、切片(`:`)、省略号(`..`)、numpy.newaxis(`None`)



我正试图找到XRF光谱中的所有峰值来拟合它们。为了合身,我将使用LMFIT GaussianModel。我有一个类型问题===>IndexError:只有整数、切片(:(、省略号(...(、numpy.newaxis(None(和整数或布尔数组才是有效的索引

下面的脚本我有:

import matplotlib.pyplot as plt
import numpy as np
from numpy import exp, pi, sqrt
from scipy.signal import find_peaks
from lmfit.models import GaussianModel, LinearModel

# Define Gaussian for fit
def gaussian(x, amplitude, center, width):
return (amplitude / (sqrt(2*pi) * width)) * exp(-(x-center)**2 / (2*width**2))
# Define function to open data file
def Read_Two_Column_File(file_name):
with open(file_name, 'r') as data:
x = []
y = []
for line in data:
p = line.split()
x.append(float(p[0]))
y.append(float(p[1]))
return x, y
# Open the data
x, y = Read_Two_Column_File('XRF_spectrum.txt')
#plot the data
plt.plot(x, y, 'b-', label='XRF_spectrum')
plt.show()
# find peaks
x_arr = np.array(x)
y_arr = np.array(y)
peaks = find_peaks(x_arr, threshold=100)
plt.plot(x_arr)
plt.plot(peaks, x_arr[peaks])
plt.show()

误差在plt.plot(peaks,x_arr[peaks](线上。我知道我不能用浮点数索引numpy数组,但我的数据是浮点数。。。我该怎么绕过这个?谢谢

scipy.signal.find_peaks返回两个值:一个具有峰值索引的peaks数组和一个properties字典。你把这些放在一个物体里。所以你可能想要:

peaks, properties = find_peaks(x_arr, threshold=100)

相关内容

最新更新