import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# example data with peaks:
x = np.linspace(-1,3,1000)
data = -0.1*np.cos(12*x)+ np.exp(-(1-x)**2)
# ___ detection of local minimums and maximums ___
a = np.diff(np.sign(np.diff(data))).nonzero()[0] + 1 # local min & max
b = (np.diff(np.sign(np.diff(data))) > 0).nonzero()[0] + 1 # local min
c = (np.diff(np.sign(np.diff(data))) < 0).nonzero()[0] + 1 # local max
# +1 due to the fact that diff reduces the original index number
# plot
plt.figure(figsize=(12, 5))
plt.plot(x, data, color='grey')
plt.plot(x[b], data[b], "o", label="min", color='r')
plt.plot(x[c], data[c], "o", label="max", color='b')