简体   繁体   中英

Find 5 local maxima and minima points plus the extremas in python

I have found some code here at Stack Overflow. However It is not working for float numbers. I am trying to find 5 local max and min points plus the extrema. Any help would be appreciated.

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

y=[ 191.78 ,   191.59,    191.59,    191.41,    191.47,    191.33,    191.25  \
  ,191.33 ,   191.48 ,   191.48,    191.51,    191.43,    191.42,    191.54    \
  ,191.5975,  191.555,   191.52 ,   191.25 ,   191.15  ,  191.01  ]
x = np.linspace(1 ,20,len(y))


fig, ax = plt.subplots(figsize=(10, 10))
ax.legend(loc='center left', bbox_to_anchor=(1.05, 0.5), frameon=False)
ax.scatter(x, y, color='black', label='data')
ax.plot(x,y,color='red')


sortId=np.argsort(x)
x=x[sortId]
y=y[sortId]

#this way the x-axis corresponds to the index of x
plt.plot(x-1,y)
plt.show()
maxm = argrelextrema(y, np.greater) 
minm = argrelextrema(y, np.less)

I did more research and the answer is using a proper array and numpy array as showing below:

 def check(self, data):
        dataarr = np.asarray(data)
        extmax = argrelextrema(dataarr, np.greater)
        extmin = argrelextrema(dataarr, np.less)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM