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

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

我希望在高原上单击鼠标可以显示并保存 y 坐标,只是为了引导我朝着正确的方向进行编程。但当我点击该图时,得到的只是一个简短的红色标记。我不想点击鼠标......谢谢,Rico。

最佳答案

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

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]

关于python - 如何让 Pyplot 识别曲线图中的高原(几乎为 0 斜率),然后打印高原的 ydata 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18970281/

相关文章:

python - 使用Python解析特定时区的日期

Python matplot-ting 使用数组附加的曲线奇怪地在较高位置显示较低值?

python - 将 np.array 索引传递给函数

python - 如何提取特定单词周围的文本片段?

python - USB 驱动器中的 PyDev 解释器指示

python - 有什么方法可以将 seaborn 中的颜色条 (cbar) 更改为图例(对于二进制热图)?

python - 在 matplotlib 中制作高度为 0 的透明色条

python - 类型错误 : an integer is required (got type str) in python

python - 如何在使用 tkinter 工具栏缩放时更改绘图的宽度

python - 在另一个函数中用作参数时,如何将自定义 Python 对象表示为字符串?