我正在尝试运用我从中学到的东西绘制多个图但具有偏移范围python https://stackoverflow.com/questions/16534551/plotting-multiple-plots-but-whith-offset-ranges-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
for N = 5, 15, 25, 51, 97
。然后我想绘制每个存储的clnsum
,但我相信这就是问题所在。
我相信
for i in csum:
是正确的设置,但是ax.plot(x, csum[i])
绘制每次迭代的方法一定是错误的。至少,这是我所相信的,但也许整个设置是错误的或有缺陷的。
我怎样才能实现每个的绘图clnsum
对于每个N
?