I have a 3d list of lists mylist
, whose shape is
(30, 30, 580)
I would like to slice it on a z
-plane, say for example at z=100
.
I have tried the following
plt.imshow(mylist[:][:][100],origin='lower', aspect='auto')
plt.show()
which produces the error
index 100 is out of bounds for axis 0 with size 30
As far as I understand, it seems as though imshow
is able to deal only with the x
and y
plane, but cannot slice on the z
plane. How to do that?
If
mylist
is a list of lists of lists, you could slice along the third axis by usingHowever, since you are using matplotlib, you must also have NumPy installed. So it would be easier to convert
mylist
into a NumPy array and then use NumPy indexing:The reason why
mylist[:][:][100]
does not work is becausemylist[:]
returns a shallow copy ofmylist
-- a new list with the exact same contents asmylist
. So bothmylist
andmylist[:][:]
are lists of lists of lists.mylist[:][:][100]
fails for the same reason thatmylist[100]
would fail -- the top level contents ofmylist
only has 30 items.