简体   繁体   English

从python中的绘图中检索数据?

[英]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. 我画了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. 现在,通过绘制这7个数据点,我想检索100个数据点并将它们保存在文本文件中。 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. 我要问的是如果我创建一个像t=np.arange(0,31,.1)这样的数组,那么f_t的相应数组与前一个图是一致的,即对于t = 0到t之间的任何t t = 7,f_t将通过使用连接(0,4)和(7,3)的直线确定,依此类推。

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. poly现在是可用于计算其他点的多项式。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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