0
我在Stack Overflow找到了一些代碼。但它不適用於浮點數。我試圖找到5個局部最大值和最小值加上極值。任何幫助,將不勝感激。查找5個局部最大值和最小值點以及python中的極值點
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)
你需要Y中一個numpy的數組,而不是蟒蛇陣列。將y行更改爲'y = np.array([191.78,...])'。 –