1
我試圖用我從plotting multiple plots but whith offset ranges python教訓,但我似乎無法使我勒繪製代碼進行適當的調整:的Python:在同一個窗口繪製多條曲線
import numpy as np
import pylab
from numpy.polynomial.legendre import leggauss, legval
def f(x):
if 0 <= x <= 1:
return 1
if -1 <= x <= 0:
return -1
f = np.vectorize(f)
deg = 1000
x, w = leggauss(deg) # len(x) == deg
L = np.polynomial.legendre.legval(x, np.identity(deg))
integral = (L * (f(x) * w)[None,:]).sum(axis=1)
xx = np.linspace(-1, 1, 500000)
csum = []
for N in [5, 15, 25, 51, 97]:
c = (np.arange(1, N) + 0.5) * integral[1:N]
clnsum = (c[:,None] * L[1:N,:]).sum(axis = 0)
csum.append(clnsum)
fig = pylab.figure()
ax = fig.add_subplot(111)
for i in csum:
ax.plot(x, csum[i])
pylab.xlim((-1, 1))
pylab.ylim((-1.25, 1.25))
pylab.plot([0, 1], [1, 1], 'k')
pylab.plot([-1, 0], [-1, -1], 'k')
pylab.show()
我使用csum
來保存clnsum
的每次迭代N = 5, 15, 25, 51, 97
。然後我想繪製每個存儲的clnsum
,但我相信這是問題發生的地方。
我相信
for i in csum:
是正確的成立,但ax.plot(x, csum[i])
必須是錯誤的方式來繪製每個迭代。至少,這是我相信的,但也許整個設置是錯誤的或錯誤的。
我該如何實現每個N
的每個clnsum
的繪圖?
十分感謝。這些選項之一是更快更有效嗎? – dustin
後者更「pythonic」,效率稍高一些(但對於處理的數量無關緊要)。 –