考虑两组数据点的最高峰曲线

Considering Highest Peak Curve From Two Sets of Data Points

我有两列对应于 x 轴和 y 轴,我最终将在其中将数据集点绘制成曲线图。

问题是,根据数据点的性质,在绘制它时,我最终得到了两个峰,但是,我想在绘制时只选择最高峰并丢弃最低峰(不是最高点但整个最高峰绘制)。

在Python有没有办法做到这一点?我没有在这里显示代码,因为我完全不确定如何进行编码。

这是数据点(输入)和图表!

您可以使用 scipy argrelextrema 获取所有峰,计算出最大值,然后为您要绘制的峰建立掩码数组。这将使您可以根据您的数据进行完全控制,使用诸如 mincutoff 之类的东西来计算出决定单独峰值的因素,

import numpy as np
from scipy.signal import argrelextrema
import matplotlib.pyplot as plt

#Setup and plot data
fig, ax = plt.subplots(1,2)
y = np.array([0,0,0,0,0,6.14,7.04,5.6,0,0,0,0,0,0,0,0,0,0,0,16.58,60.06,99.58,100,50,0.,0.,0.])
x = np.linspace(3.92,161,y.size)
ax[0].plot(x,y)

#get peaks
peaks_indx = argrelextrema(y, np.greater)[0]
peaks = y[peaks_indx]
ax[0].plot(x[peaks_indx],y[peaks_indx],'o')

#Get maxpeak
maxpeak = 0.
for p in peaks_indx:
    print(p)
    if y[p] > maxpeak:
        maxpeak = y[p]
        maxpeak_indx = p

#Get mask of data around maxpeak to plot
mincutoff = 0.
indx_to_plot = np.zeros(y.size, dtype=bool)
for i in range(maxpeak_indx):
    if y[maxpeak_indx-i] > mincutoff:
        indx_to_plot[maxpeak_indx-i] = True
    else:
        indx_to_plot[maxpeak_indx-i] = True
        break

for i in range(y.size-maxpeak_indx):
    if y[maxpeak_indx+i] > mincutoff:
        indx_to_plot[maxpeak_indx+i] = True
    else:
        indx_to_plot[maxpeak_indx+i] = True
        break
ax[1].plot(x[indx_to_plot],y[indx_to_plot])
plt.show()

那么结果就是,

更新:仅绘制最大峰的代码。

import numpy as np
from scipy.signal import argrelextrema
import matplotlib.pyplot as plt

#Setup and plot data
y = np.array([0,0,0,0,0,6.14,7.04,5.6,0,0,0,0,0,0,
              0,0,0,0,0,16.58,60.06,99.58,100,50,0.,0.,0.])
x = np.linspace(3.92,161,y.size)

#get peaks
peaks_indx = argrelextrema(y, np.greater)[0]
peaks = y[peaks_indx]

#Get maxpeak
maxpeak = 0.
for p in peaks_indx:
    print(p)
    if y[p] > maxpeak:
        maxpeak = y[p]
        maxpeak_indx = p

#Get mask of data around maxpeak to plot
mincutoff = 0.
indx_to_plot = np.zeros(y.size, dtype=bool)
for i in range(maxpeak_indx):
    if y[maxpeak_indx-i] > mincutoff:
        indx_to_plot[maxpeak_indx-i] = True
    else:
        indx_to_plot[maxpeak_indx-i] = True
        break

for i in range(y.size-maxpeak_indx):
    if y[maxpeak_indx+i] > mincutoff:
        indx_to_plot[maxpeak_indx+i] = True
    else:
        indx_to_plot[maxpeak_indx+i] = True
        break

#Plot just the highest peak
plt.plot(x[indx_to_plot],y[indx_to_plot])
plt.show()

我仍然建议绘制两个峰值以确保算法正常工作...我认为您会发现识别任意峰值可能并不总是对杂乱的数据微不足道。