python - 检测 python 图中的峰值

标签 python python-3.x numpy

我的数据文件在以下链接中共享。

我们可以使用以下脚本绘制此数据。

import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook

def read_datafile(file_name):

    data = np.loadtxt(file_name, delimiter=',')
    return data

data = read_datafile('mah_data.csv')


fig = plt.figure()

ax1 = fig.add_subplot(111)

ax1.set_title("Data")    
ax1.set_xlabel('t')
ax1.set_ylabel('s')

ax1.plot(x,y, c='r', label='My data')

leg = ax1.legend()

plt.show()

我们如何检测 python 中的峰值?我在 Python 中找不到合适的峰值检测算法。

最佳答案

您可以使用 scipy.signal 中的 argrelextrema 函数返回数组的局部最大值或局部最小值的索引。通过指定轴,这也适用于多维数组。

from scipy.signal import argrelextrema

ind_max = argrelextrema(z, np.greater) # indices of the local maxima
ind_min = argrelextrema(z, np.less)  # indices of the local minima

maxvals = z[ind_max]
minvals = z[ind_min]

更具体地说,可以使用argrelmaxargrelmin 来找到局部最大值或局部最小值。这也适用于使用轴参数的多维数组。

from scipy.signal import argrelmax, argrelmin

ind_max = argrelmax(z, np.greater) # indices of the local maxima
ind_min = argrelmin(z, np.less)  # indices of the local minima

maxvals = z[ind_max]
minvals = z[ind_min]

更多详情,可引用此链接:https://docs.scipy.org/doc/scipy/reference/signal.html#peak-finding

关于python - 检测 python 图中的峰值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44466310/

相关文章:

python - lxml 和循环在 python 中创建 xml rss

python - 使用没有元数据的 start_url 将额外值传递给 Scrapy 蜘蛛

python - 为什么 matplotlib spins 有 "set_label"和 "get_label"方法?

python - 金钱和类型错误: __init__() takes from 1 to 2 positional arguments but 3 were given

Python - 从txt文件导入列表,迭代并作为参数传递

python - Python中隐式曲线的高阶局部插值

python-3.x - “ float ”对象是不可切片的

c++ - Cython/C++ - 共享 PXD 文件中的头文件路径

python - 迭代两个 numpy 数组返回一维数组

python - 使用 Twisted 或使用线程、Python 中的队列处理大量流数据