How to extract points from a graph?

前端 未结 3 1373
北恋
北恋 2020-12-25 10:37

I have a question.

I have plotted a graph using Matplotlib like this:

from matplotlib import pyplot
import numpy
from scipy.interpolate import spline         


        
相关标签:
3条回答
  • 2020-12-25 10:56

    Here's another option if you're willing to use a different spline function:

    from matplotlib import pyplot
    import numpy
    from scipy import interpolate
    
    widths = numpy.array([0, 30, 60, 90, 120, 150, 180])
    heights = numpy.array([26, 38.5, 59.5, 82.5, 120.5, 182.5, 319.5])
    
    xnew = numpy.linspace(widths.min(),widths.max(),300)
    heights_smooth = interpolate.splrep(widths,heights) #Use splrep instead of spline
    
    #Select desired width values
    width_vals = [0, 80.5, 38.98743]   
    
    #splev returns the value of your spline evaluated at the width values.    
    heights = interpolate.splev(width_vals, heights_smooth)
    

    Then

    In[]:  heights
    Out[]: array([ 26.        ,  74.1721985 ,  44.47929453])
    

    Or evaluate at a point:

    w = 167.2
    heights = interpolate.splev(w, heights_smooth)
    height = heights.item()
    
    In[]:  height
    Out[]: 247.8396196684303
    

    The .item() function is necessary because splev returns an array()

    0 讨论(0)
  • 2020-12-25 11:01

    plot() returns a useful object: [<matplotlib.lines.Line2D object at 0x38c9910>]
    From that we can get x- and y-axis values:

    import matplotlib.pyplot as plt, numpy as np
    ...
    line2d = plt.plot(xnew,heights_smooth)
    xvalues = line2d[0].get_xdata()
    yvalues = line2d[0].get_ydata()
    

    Then we can get the index of one of the width values:

    idx = np.where(xvalues==xvalues[-2]) # this is 179.3979933110368
    # idx is a tuple of array(s) containing index where value was found
    # in this case -> (array([298]),)
    

    And the corresponding height:

    yvalues[idx]
    # -> array([ 315.53469])
    

    To check we can use get_xydata():

    >>> xy = line2d[0].get_xydata()
    >>> xy[-2]
    array([ 179.39799331,  315.53469   ])
    
    0 讨论(0)
  • 2020-12-25 11:04

    You could cast the array to a list:

    >>> heights[list(widths).index(30)]
    38.5
    

    for the interpolated result:

    s = xnew[56] 
    print s, heights_smooth[list(xnew).index(s)]
    33.7123745819, 40.9547542163
    

    As xnew is an ordered list you could use the bisect module to find a closest width value for a queried width, and then find the corresponding height, in a similar fashion:

    ....
    import bisect
    pyplot.plot(xnew,heights_smooth)
    #33.1222 is a queried value which does not exist in xnew.
    index_of_nearest_width = bisect.bisect_left(xnew, 33.1222) 
    width_val = xnew[index_of_closest_width]
    print width_val, heights_smooth[list(xnew).index(width_val)]
    #prints the nearest width to 33.1222 then the corresponding height.
    33.7123745819 40.9547542163
    
    0 讨论(0)
提交回复
热议问题