需要修改函数以生成子批次矩阵

2024-06-07 03:34:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我在代码中使用了以下峰值检测功能: https://nbviewer.jupyter.org/github/demotu/BMC/blob/master/notebooks/DetectPeaks.ipynb 此函数的最后20行左右与打印有关,打印是使用show=True参数自动完成的。 我有18个数据文件,我想修改这个函数(如果需要的话),以获得6x3子批次的峰值检测图,类似于链接中显示的那些,而不是单独生成它们

以下是我已经尝试过的:

fig, axes = plt.subplots(nrows=6, ncols=3, figsize=(20,20))
for i, ax in enumerate(axes.flatten()):
    for idx, file in enumerate(files):
        if i == idx:
            detect_peaks(P[file], show=True)

以下是我可能需要更改的函数部分:

def _plot(x, mph, mpd, threshold, edge, valley, ax, ind):
    """Plot results of the detect_peaks function, see its help."""
    try:
        import matplotlib.pyplot as plt
    except ImportError:
        print('matplotlib is not available.')
    else:
        if ax is None:
            _, ax = plt.subplots(1, 1, figsize=(8, 4))

        ax.plot(x, 'b', lw=1)
        if ind.size:
            label = 'valley' if valley else 'peak'
            label = label + 's' if ind.size > 1 else label
            ax.plot(ind, x[ind], '+', mfc=None, mec='r', mew=2, ms=8,
                    label='%d %s' % (ind.size, label))
            ax.legend(loc='best', framealpha=.5, numpoints=1)
        ax.set_xlim(-.02*x.size, x.size*1.02-1)
        ymin, ymax = x[np.isfinite(x)].min(), x[np.isfinite(x)].max()
        yrange = ymax - ymin if ymax > ymin else 1
        ax.set_ylim(ymin - 0.1*yrange, ymax + 0.1*yrange)
        ax.set_xlabel('Data #', fontsize=14)
        ax.set_ylabel('Amplitude', fontsize=14)
        mode = 'Valley detection' if valley else 'Peak detection'
        ax.set_title("%s (mph=%s, mpd=%d, threshold=%s, edge='%s')"
                     % (mode, str(mph), mpd, str(threshold), edge))
        # plt.grid()
        plt.show()

这将生成一个空的6x3子批次矩阵,每个峰值检测图分别显示在其下方

我要找的是这样的东西:

https://imgur.com/U46eF3e


Tags: 函数sizeifplotshowpltaxelse