2017-08-31 37 views
0

我正在尝试绘制一个图。在X轴上,我想看到我的FinalSigmaValues字典的键,在Y轴上,我想看到同一个字典的值。我希望他们创造一条平滑的曲线。同时,我还有另外一本与FinalSigmaValues具有相同密钥的字典。所以,我也想在同一个地块上绘制另一条曲线。我正在使用下面的代码,并得到很多错误。在一个图上绘制两条曲线

from scipy.interpolate import spline 
import matplotlib.pyplot as plt 
import numpy as np 
import collections 
Finalsigma = collections.OrderedDict(sorted(FinalSigmaValues.items())) 
P = np.array(Finalsigma.keys()) 
T = np.array(Finalsigma.values()) 
xnew = np.linspace(T.min(),T.max(),300) 
P_smooth = spline(T,P,xnew) 
plt.plot(xnew,P_smooth,color='k') 
plt.xlabel('w') 
plt.ylabel('First Part of the Objective Function') 
plt.show() 

正如你在代码中看到,我目前只想要绘制FinalSigmaValues,我也有“FinalPhiValues”的担心。这两本词典都在len 1376.任何建议表示赞赏。

--------------------------------------------------------------------------- 
LinAlgError        Traceback (most recent call last) 
<ipython-input-18-2d2343ec9634> in <module>() 
     4 xnew = np.linspace(T.min(),T.max(),300) 
     5 
----> 6 P_smooth = spline(T,P,xnew) 
     7 
     8 plt.plot(xnew,P_smooth,color='k') 

C:\Users\administrater\Anaconda2\lib\site-packages\scipy\interpolate\interpolate.pyc in spline(xk, yk, xnew, order, kind, conds) 
    3010 
    3011  """ 
-> 3012  return spleval(splmake(xk,yk,order=order,kind=kind,conds=conds),xnew) 

C:\Users\administrater\Anaconda2\lib\site-packages\scipy\interpolate\interpolate.pyc in splmake(xk, yk, order, kind, conds) 
    2925  # the constraint matrix 
    2926  B = _fitpack._bsplmat(order, xk) 
-> 2927  coefs = func(xk, yk, order, conds, B) 
    2928  return xk, coefs, order 
    2929 

C:\Users\administrater\Anaconda2\lib\site-packages\scipy\interpolate\interpolate.pyc in _find_smoothest(xk, yk, order, conds, B) 
    2622  tmp = dot(V2.T,A) 
    2623  Q = dot(tmp,V2) 
-> 2624  p = scipy.linalg.solve(Q, tmp) 
    2625  tmp = dot(V2,p) 
    2626  tmp = np.eye(N+K) - tmp 

C:\Users\administrater\Anaconda2\lib\site-packages\scipy\linalg\basic.pyc in solve(a, b, sym_pos, lower, overwrite_a, overwrite_b, debug, check_finite) 
    101   return x 
    102  if info > 0: 
--> 103   raise LinAlgError("singular matrix") 
    104  raise ValueError('illegal value in %d-th argument of internal gesv|posv' % 
    105      -info) 

LinAlgError: singular matrix 

另外,我想:

from scipy.interpolate import spline 
import matplotlib.pyplot as plt 
import numpy as np 
import collections 
Finalsigma = collections.OrderedDict(sorted(FinalSigmaValues.items())) 
Finalphi = collections.OrderedDict(sorted(FinalPhiValues.items())) 

from scipy.interpolate import interp1d 

x = Finalsigma.keys() 
y = Finalsigma.values() 
f = interp1d(x, y) 
f2 = interp1d(x, y, kind='cubic') 

xnew = Finalphi.values() 
plt.plot(x, y, 'o', xnew, f(xnew), '-', xnew, f2(xnew), '--') 
plt.show() 

--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
<ipython-input-37-51a1312588ca> in <module>() 
    14 
    15 xnew = Finalphi.values() 
---> 16 plt.plot(x, y, 'o', xnew, f(xnew), '-', xnew, f2(xnew), '--') 
    17 plt.show() 

C:\Users\administrater\Anaconda2\lib\site-packages\scipy\interpolate\polyint.pyc in __call__(self, x) 
    77   """ 
    78   x, x_shape = self._prepare_x(x) 
---> 79   y = self._evaluate(x) 
    80   return self._finish_y(y, x_shape) 
    81 

C:\Users\administrater\Anaconda2\lib\site-packages\scipy\interpolate\interpolate.pyc in _evaluate(self, x_new) 
    586   y_new = self._call(self, x_new) 
    587   if not self._extrapolate: 
--> 588    below_bounds, above_bounds = self._check_bounds(x_new) 
    589    if len(y_new) > 0: 
    590     # Note fill_value must be broadcast up to the proper size 

C:\Users\administrater\Anaconda2\lib\site-packages\scipy\interpolate\interpolate.pyc in _check_bounds(self, x_new) 
    618     "range.") 
    619   if self.bounds_error and above_bounds.any(): 
--> 620    raise ValueError("A value in x_new is above the interpolation " 
    621     "range.") 
    622 

ValueError: A value in x_new is above the interpolation range. 
+0

“获得了大量的错误的”,是不足够的描述。请提供确切的错误消息。不要忘记将它们格式化为代码。 – ForceBru

+0

对不起,忘了补充一点。现在,它被添加。 – user8028576

回答

0

你为什么不尝试类似如下(代码https://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html拾起)

from scipy.interpolate import interp1d 

x = np.linspace(0, 10, num=11, endpoint=True) 
y = np.cos(-x**2/9.0) 
f = interp1d(x, y) 
f2 = interp1d(x, y, kind='cubic') 

xnew = np.linspace(0, 10, num=41, endpoint=True) 
import matplotlib.pyplot as plt 
plt.plot(x, y, 'o', xnew, f(xnew), '-', xnew, f2(xnew), '--') 
plt.legend(['data', 'linear', 'cubic'], loc='best') 
plt.show() 
+0

无法真正了解如何使用它。请看看我如何尝试使用它。它仍然会给出错误。 – user8028576