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 using
[[mylist[i][j][100] for j in range(30)] for i in range(30)]
However, 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:
myarrary = np.array(mylist)
plt.imshow(myarray[..., 100], origin='lower', aspect='auto')
The reason why mylist[:][:][100]
does not work is because mylist[:]
returns a shallow copy of mylist
-- a new list with the exact same contents as mylist
. So both mylist
and mylist[:][:]
are lists of lists of lists.
mylist[:][:][100]
fails for the same reason that mylist[100]
would fail -- the top level contents of mylist
only has 30 items.