简体   繁体   English

我如何 label 来自 scipy.signal.find_peaks 的峰值及其值?

[英]How do I label the peaks from scipy.signal.find_peaks with their value?

I can easily get scipy.signal.find_peaks to plot peaks (example from the documentation):我可以很容易地获得scipy.signal.find_peaks到 plot 峰(文档中的示例):

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()

在此处输入图像描述

And getting the values of peaks is also easy:获取峰值的值也很容易:

在此处输入图像描述

But how can I plot x[peaks] either in place of the dot or nearby?但是我怎么能用 plot x[peaks]代替点或在点附近呢?

I had expected plt.plot(peaks, x[peaks], x[peaks]) would have worked, but no.我原plt.plot(peaks, x[peaks], x[peaks])会起作用,但没有。

The only workable solution I found was using the more upvoted answers here that J. Choi referenced我找到的唯一可行的解决方案是使用 J. Choi 在这里引用的更受欢迎的答案

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:结果是: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM