我有两列对应于x和y轴,其中我将最终将这些数据点集绘制成曲线图。
问题是基于数据点的性质,在绘制图形时,我最终得到两个峰值,但是我想在绘制图形时仅选择最高峰值并丢弃最低峰值(不是最高点)但整个最高峰画出来了)。
在Python中有没有这样做?我没有在这里显示代码,因为我不知道如何编码。
这是数据点(输入)以及图表!
答案 0 :(得分:1)
您可以使用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()
我仍然建议绘制两个峰值以确保算法正常工作...我认为你会发现识别任意峰值可能并不总是对于混乱的数据而言是微不足道的。