我正在尝试使用从plotting multiple plots but whith offset ranges python中学到的知识,但似乎无法对Legendre绘图代码进行适当的调整:

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])必须是绘制每次迭代的错误方法。至少,这是我所相信的,但也许整个设置是错误的或错误的。

如何实现每个clnsum的每个N的绘图?

最佳答案

for i in csum:
    ax.plot(x, csum[i])


这是您的问题所在。我不是整数,而是一个数组。你可能是说

for i in range(len(csum)):


你也可以

for y in csum:
    ax.plot(x, y)

关于python - Python:在同一窗口中绘制多个图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19164286/

10-12 17:02
查看更多