2014-01-07 136 views
0

我已经计算了散点图的最佳拟合曲线,并且我想将结果绘制为平滑曲线,类似于SAS的样条。使用scipy.interplote.interp1d和matplotlib绘制平滑曲线Python 2.7 32位(Enthought Canopy)

经过一番谷歌搜索后,发现我应该先在我的数据上使用interpolate.interp1d,然后再绘制线条。但是,当我尝试基于documentation中的教程来执行此操作时,出现错误。提前感谢任何帮助或资源!

from scipy import interpolate 
j = np.arange(0, 29, 1) # new x values 
k = (model(xdata, g_fit, a_fit, b_fit)) # y values 
l = interpolate.interp1d(j, k) 

plt.scatter(xdata, ydata, c='g', marker='x') 
plt.plot(xdata, model(xdata, g_fit, a_fit, b_fit), color='red') 
plt.plot(j, l(k)) 
plt.axis([-1, 31, 0.5, 1.2]) # xmin, xmax, ymin, ymax 
plt.show() 
print p 


--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
<ipython-input-56-0db707080a49> in <module>() 
     2 j = np.arange(0, 29, 1) 
     3 k = (model(xdata, g_fit, a_fit, b_fit)) 
----> 4 l = interpolate.interp1d(j, k) 
     5 
     6 plt.scatter(xdata, ydata, c='g', marker='x') 

C:\Enthought\Canopy32\User\lib\sitepackages\scipy\interpolate\ 
interpolate.py in __init__ 
(self, x, y, kind, axis, copy, bounds_error, fill_value) 
    331     copy=True, bounds_error=True, fill_value=np.nan): 
    332   """ Initialize a 1D linear interpolation class.""" 
--> 333   _Interpolator1D.__init__(self, x, y, axis=axis) 
    334 
    335   self.copy = copy 

C:\Enthought\Canopy32\User\lib\site-packages\scipy\interpolate\ 
polyint.py in __init__(self, xi, yi, axis) 
    33   self.dtype = None 
    34   if yi is not None: 
---> 35    self._set_yi(yi, xi=xi, axis=axis) 
    36 
    37  def __call__(self, x): 

C:\Enthought\Canopy32\User\lib\site-packages\scipy\interpolate\ 
polyint.py in _set_yi(self, yi, xi, axis) 
    92    shape = (1,) 
    93   if xi is not None and shape[axis] != len(xi): 
---> 94    raise ValueError("x and y arrays must be equal in length along " 
    95        "interpolation axis.") 
    96 

ValueError: x and y arrays must be equal in length along interpolation axis. 

回答

0

WH不要你第一次检查,他塑造的阵列Ĵ的。也许你的功能模型 returs不同大小的数组?

我真的应该提出一个意见,但我有足够的点张贴评论...

+0

我想,我也许已经做了一些错误,当我试图interpolate.interp1d和np.arange,我不知道每一步应该做什么。每个y值都有一个x值,你问的是什么? – Bprodz

+0

错误_ValueError:x和y数组沿着插值axis_的长度必须相等。说我和j的形状不一样。但是我有广告poblems与特定功能之前进行三次插值。对于你的情况不应该如此。 – ssm