views:

30

answers:

1

Hello! I have a 3-D array ar.

print shape(ar)  # --> (81, 81, 256) 

I want to plot this array.

fig = plt.figure()
ax1 = fig.add_subplot(111)
for i in arange(256):
    im1 = ax1.imshow(ar[:][:][i])
    plt.draw()
    print i

I get this error-message:

    im1 = ax1.imshow(ar[:][:][i])
IndexError: list index out of range

Why do I get this strange message? The graph has the size 81 x 256 and not like expected 81 x 81. But why?

+2  A: 

Do:

ar[:,:,i]

The syntax ar[:] makes a copy of ar (slices all its elements), so ar[:][:][i] is semantically equivalent to ar[i]. This is an 81*256 matrix, since ndarrays are nested lists.

katrielalex
then I get an other error: TypeError: list indices must be integers, not tuple
kame
@kame: Are you not using numpy here? If not, you should be. Specifically, I think `ar` is a list of lists of lists instead of a numpy array. You can cast it to an array with `ar = np.array( ar )`.
katrielalex