user22180
user22180

Reputation: 561

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, i.e., 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.

Upvotes: 1

Views: 109

Answers (2)

Ray
Ray

Reputation: 4671

The function np.interp will do linear interpolation between your data points:

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

Upvotes: 1

Stephane Rolland
Stephane Rolland

Reputation: 39906

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)

Upvotes: 2

Related Questions