我正在嘗試繪製一個圖。在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.
「獲得了大量的錯誤的」,是不足夠的描述。請提供確切的錯誤消息。不要忘記將它們格式化爲代碼。 – ForceBru
對不起,忘了補充一點。現在,它被添加。 – user8028576