0

I'm trying to figure out how to plot the peaks of a simple histogram using scipy.signal.find_peaks but the peaks found seem way off.

ages = np.array([10, 5, 22, 13, 50, 45, 67, 30, 21, 34, 60, 67, 89, 45, 45, 65])
hist, bin_edges = np.histogram(ages, 10)
bin_edges = bin_edges[1:]

plt.plot(bin_edges, hist)
peaks, _ = find_peaks(hist)
plt.plot(ages[peaks], peaks, "x")

plotted peaks

1 Answer 1

4

You should try:

plt.plot(bin_edges[peaks], hist[peaks], "x")

find_peaks gives you the indices of local maxima in the hist signal.

The x-values of your histogram are bin_edges and the y-values are given by hist. So you have to look for the indices given by peaks in each of these series.

Sign up to request clarification or add additional context in comments.

3 Comments

Wow that looks great! Thank you! That looks like the right answer to me. Can you add an explanation as to what this line is doing? How is it relating the peaks to the x and y since peaks is a 1d array?
I updated the answer with a bit more explanation :)
It is worth noting that histograms are not always the best way to visualise a distribution, and can lead to misleading interpretations of the data: a simple change of bin width can drastically change their form. Depending on your application, a better(?) way may be to calculate the "kernel density estimation" (see jakevdp.github.io/blog/2013/12/01/kernel-density-estimation) though, of course, in this example it might be overkill!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.