How to extract points from a graph?

前端 未结 3 1372
北恋
北恋 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 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
    

提交回复
热议问题