我正在努力制作一个情节。在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 :(得分: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()