简体   繁体   中英

retrieving data from a plot in python?

suppose I have

t= [0,7,10,17,23,29,31]

f_t= [4,3,11,19,12,9,17]

and I have plotted f_t vs t.

Now from plotting these 7 data points, I want to retrieve 100 data points and save them in a text file. What do I have to do?

Note that I am not asking about the fitting of the plot; I know between two points the plot is linear.

What I am asking If I create a array like t=np.arange(0,31,.1) , then what is the corresponding array of f_t which agrees well with the previous plot, ie, for any t between t=0 to t=7, f_t will be determined by using a straight line connecting (0,4) and (7,3), and so on.

You should use a linear regression, that gives you a straight line formula, in which you can grasp as many points as you want.

If the line is more of a curve, then you should try to have a polynomial regression of higher degree.

ie:

import pylab
import numpy

py_x =  [0,7,10,17,23,29,31]

py_y = [4,3,11,19,12,9,17] 

x = numpy.asarray(py_x)
y = numpy.asarray(py_y)

poly = numpy.polyfit(x,y,1) # 1 is the degree here. If you want curves, put 2, 3 or 5...

poly is now the polynome you can use to calculate other points with.

for z in range(100):
    print numpy.polyval(poly,z) #this returns the interpolated f(z)

函数np.interp将在您的数据点之间进行线性插值:

f2 = np.interp(np.arange(0,31,.1), t, ft)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM