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”,效率稍高一些(但对于处理的数量无关紧要)。 –