Python:在同一窗口中绘制多个图 [英] Python: plotting multiple plots in the same window
问题描述
我正在尝试使用我从 绘制多个图但偏移范围是python ,但是我似乎无法对Legendre绘图代码进行适当的调整:
I am trying to use what I learned from plotting multiple plots but whith offset ranges python, but I can't seem to make the appropriate adjustments for my Legendre plotting code:
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
,但我相信这是发生问题的地方.
I am using csum
to hold each iteration of clnsum
for N = 5, 15, 25, 51, 97
. Then I want to plot each stored clnsum
, but I believe this is where the problem is occurring.
我相信
for i in csum:
是正确的设置,但 ax.plot(x, csum[i])
必须是绘制每次迭代的错误方式.至少,这是我所相信的,但也许整个设置是错误的或有缺陷的.
is the correct set up but ax.plot(x, csum[i])
must be the wrong way to plot each iteration. At least, this is what I believe, but maybe the whole set up is wrong or faulty.
如何为每个N
实现每个clnsum
的绘图?
How can I achieve the plotting of each clnsum
for each N
?
推荐答案
for i in csum:
ax.plot(x, csum[i])
这就是您的问题所在.i 不是整数,它是一个数组.你可能是说
This is where your problem is. i is not an integer, it's an array. You probably mean
for i in range(len(csum)):
你也可以
for y in csum:
ax.plot(x, y)
这篇关于Python:在同一窗口中绘制多个图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!