Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get Pyplot to recognise a plateau (almost 0 slope) in a curved line plot, then print ydata value of plateau?

I have a Python program that shows a plot of a descending temperature vs time. Along the descent the temperature remains constant for awhile, almost 0 slope, then continues to decrease. Its this area in the curve when the temperature is constant that I would like the program to automatically detect and show the y value. This value will later be put into an equation. I'm trying to find out how to do this. I've tried and failed, my last attempt was:

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

I was hoping a mouseclick on the plateau would show and save the y coordinate, just to lead me in the right direction as far as programming. But all this got was a brief red marker when I clicked on the plot. I don't want to have to mouseclick....Thanks, Rico.

like image 829
Rico Avatar asked Oct 21 '22 00:10

Rico


1 Answers

iterate over small chunks of the data, determine the slope of the chunk, return the point that meets your criteria

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]
like image 77
wwii Avatar answered Oct 31 '22 16:10

wwii