如何让 Pyplot 识别曲线图中的高原(几乎为 0 斜率),然后打印高原的 ydata 值?

Ric*_*ico 5 python matplotlib

我有一个 Python 程序,它显示了下降温度与时间的关系图。随着下降,温度保持恒定一段时间,几乎为 0 斜率,然后继续下降。当温度恒定时,曲线中的这个区域是我希望程序自动检测并显示 y 值的区域。这个值稍后会被放入一个等式中。我试图找出如何做到这一点。我尝试过但失败了,我最后一次尝试是:

import numpy as np
import matplotlib.pyplot as plt
list_of_files=[('logfile.txt', 'temp')]
datalist = [ ( np.loadtxt(filename), label ) for filename, label in list_of_files]
for data, label in datalist:
    plt.plot( data[:0], data[:,1], label=label )
    plt.ginput(n=1, timeout=30, show_clicks=True, mouse_add=1, mouse_pops=3, mouse_stop=2)
    plt.show()
Run Code Online (Sandbox Code Playgroud)

我希望在高原上单击鼠标会显示并保存 y 坐标,只是为了引导我朝着正确的方向进行编程。但是当我点击情节时,所有这些只是一个简短的红色标记。我不想用鼠标点击....谢谢,Rico。

wwi*_*wii 3

迭代小数据块,确定数据块的斜率,返回满足您的条件的点

def zero_slope(data, chunksize = 3, max_slope = .001):
    """return the 'first' data point with zero slope

    data --> numpy ndarray - 2d [[x0,y0],[x1,y1],...]
    chunksize --> odd int
    returns numpy ndarray
    """
    midindex = chunksize / 2
    for index in xrange(len(data) - chunksize):
        chunk = data[index : index + chunksize, :]
        # subtract the endpoints of the chunk
        # if not sufficient, maybe use a linear fit
        dx, dy = abs(chunk[0] - chunk[-1])
        print dy, dx, dy / dx
        if 0 <= dy / dx < max_slope:
            return chunk[midindex]
Run Code Online (Sandbox Code Playgroud)