I have this code:
import matplotlib.pyplot as plt
from scipy.signal import find_peaks, peak_prominences
# I have my array x
peaks, _ = find_peaks(x)
prominences, left_bases, right_bases = peak_prominences(x, peaks)
contour_heights = x[peaks] - prominences
plt.plot(x)
plt.plot(peaks, x[peaks], "x")
plt.vlines(x=peaks, ymin=contour_heights, ymax=x[peaks])
plt.hlines(contour_heights, xmin=left_bases, xmax=right_bases)
plt.show()
And the resulting image is this:

Indeed, left_bases = array([ 4, 10, 16, 22, 29, 36, 42]) whereas right_bases = array([47, 47, 47, 29, 47, 47, 47]).
Indeed, this is not exactly what I was looking, I was expecting for example the right value of right_bases[0] = 10 for example. Why is this happening? How does the code determine right and left bases?
Why is this happening? How does the code determine right and left bases?
The manual page on peak_prominences() explains how this is done.
Strategy to compute a peak’s prominence:
- Extend a horizontal line from the current peak to the left and right until the line either reaches the window border (see wlen) or intersects the signal again at the slope of a higher peak. An intersection with a peak of the same height is ignored.
- On each side find the minimal signal value within the interval defined above. These points are the peak’s bases.
- The higher one of the two bases marks the peak’s lowest contour line. The prominence can then be calculated as the vertical difference between the peaks height itself and its lowest contour line.
In other words, to find the value of right_bases[0], start with the point at x=8. Search right from this point until one of three things happens:
Once it has that window, it looks for the minimal value in that window. The index of the minimal value is the value of right_bases for this peak. A similar strategy is done for left_bases.
You may want to consider using wlen to control the window size.