索引器错误:尝试在Matplotlib中绘图时索引超出界限

2024-09-28 16:20:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将下面的代码从MATLAB中Trefethen的谱方法转换成Python。但是遇到以下关于索引越界的错误。我有点困惑什么索引是越界的,以及如何修复它。任何帮助都将不胜感激。

错误

Traceback (most recent call last):
  File "C:\Documents and Settings\My Documents\Computational Physics\Wave-eqn.py", line 56, in <module>
ax.plot_wireframe(x,tdata,data,rstride=10, cstride=10)
  File "C:\Python32\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 906, in  plot_wireframe
    tylines = [tY[i] for i in cii]
  File "C:\Python32\lib\site-packages\mpl_toolkits\mplot3d\axes3d.py", line 906, in  <listcomp>
    tylines = [tY[i] for i in cii]
IndexError: index out of bounds

特雷费申密码

% p6.m - variable coefficient wave equation using differentiation matrices

% Grid, variable coefficient, and initial data:
N = 512; h = 2*pi/N; x = h*(1:N); t = 0; dt = h/4;
a = .1;
c = a + sin (x-1).^2;
v = exp(-100*(x-1).^2); vold = exp(-100*(x-a*dt-1).^2);

column = [0 .5*(-1).^(1:N-1).*cot((1:N-1)*h/2)];
D = toeplitz(column,-column);

% Time-stepping by leap frog formula:
tmax = 15; tplot = .15; clf, drawnow, set(gcf,'renderer','zbuffer')
plotgap = round(tplot/dt); dt = tplot/plotgap;
nplots = round(tmax/tplot);
data = [v; zeros(nplots,N)]; tdata = t;
for i = 1:nplots
 for n = 1:plotgap
        t = t+dt;
        %       v_hat = fft(v);
        %       w_hat = 1i*[0:N/2-1 0 -N/2+1:-1] .* v_hat;
        %       w = real(ifft(w_hat));
        w = (D*v')';
        vnew = vold - 2*dt*c.*w; vold = v; v = vnew;
    end
    data(i+1,:) = v; tdata = [tdata; t];
end
waterfall(x,tdata,data), view(10,70), colormap(1e-6*[1 1 1]);
axis([0 2*pi 0 tmax 0 3]), ylabel t, zlabel u, grid off

我的代码

import numpy as np
from numpy import *
from math import  pi
from scipy.linalg import toeplitz
from scipy.special import cotdg
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt


N = 512
h = (2*np.pi)/N
x = h*(np.arange(N)+1)
t = 0
dt = h/4.
a = .1
c = a + np.sin(x - 1)**2
v = np.exp(-100 * (x - 1)**2)
vold = np.exp(-100 * (x - a*dt - 1)**2)

column = ((0.5*(-1)**arange(N))*cotdg(arange(N))*(h/2));
D = toeplitz(column,-column);
#print(D.shape);


tmax = 15   
tplot = .15
plotgap = int(around(tplot/dt));print(plotgap)
dt = tplot/plotgap
nplots = int(round((tmax/tplot)));print(nplots)
k = np.zeros(((nplots,N))) 
data = np.concatenate((v.reshape((512,1)).transpose(), k))
tdata = t

for i in range(nplots):
    for n in range(plotgap):
        t = t+dt
        w = (D*v)
        vnew = vold-2*dt*c*w
        vold = v
        v = vnew
    data[i,:] = v[0,:]
    tdata = vstack([tdata, t])
print('shape data =',data.shape)
print('shape v =',v.shape)
print('shape tdata =',tdata.shape)
print('shape x =',x.shape)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_wireframe(x,tdata,data,rstride=10, cstride=10)
plt.show()

python完成的数组形状

 shape data = (101, 512)
 shape tdata = (101, 1)
 shape x = (512,)

我有一个朋友在MatLab中为这段代码运行size()命令,他为数组设计了这些形状

data =  101 512
tdata = 101 1
x =     1   512

Tags: inimportfordatanpdtcolumnprint
2条回答

plot_wireframe的前两个参数需要是2D数组。Documentation

我不知道如何定制你的代码(因为有很多),但我希望这能有所帮助。

编辑:尝试^{}查看有效输入的外观示例。

在matplotlib-users@lists.sourceforge.net的帮助下,我找到了答案。阵列

x and tdata

需要广播所以我做了

X,Y = np.broadcast_arrays(x,tdata)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_wireframe(X,Y,data,rstride=5, cstride=5)
plt.show()

相关问题 更多 >