0

I can easily get scipy.signal.find_peaks to plot peaks (example from the documentation):

import matplotlib.pyplot as plt
from scipy.misc import electrocardiogram
from scipy.signal import find_peaks

x = electrocardiogram()[2000:4000]

peaks, _ = find_peaks(x, distance=150)
np.diff(peaks)

plt.plot(x)
plt.plot(peaks, x[peaks], '.')
plt.show()

enter image description here

And getting the values of peaks is also easy:

enter image description here

But how can I plot x[peaks] either in place of the dot or nearby?

I had expected plt.plot(peaks, x[peaks], x[peaks]) would have worked, but no.

hrokr
  • 3,276
  • 3
  • 21
  • 39
  • Do you mean add text boxes with `x[peaks]` values on your plot? If so, see https://stackoverflow.com/questions/14432557/scatter-plot-with-different-text-at-each-data-point – J. Choi Jul 08 '22 at 04:59
  • That's a way of doing it but I'm surprised there isn't something more idiomatic. – hrokr Jul 08 '22 at 13:59
  • Try `plt.text` function. Docs: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.text.html – dankal444 Jul 11 '22 at 12:41

1 Answers1

0

The only workable solution I found was using the more upvoted answers here that J. Choi referenced

fig, ax = plt.subplots()
ax.scatter(p, q)
plt.gcf().set_size_inches(20, 10)
for i, txt in enumerate(p):
    ax.annotate(txt, (p[i], q[i]))

plt.plot(x)
plt.plot(peaks, x[peaks], 'x')
plt.show()

Results in: enter image description here

hrokr
  • 3,276
  • 3
  • 21
  • 39