python numpy print array(x,y,z) as (x by y) by z?

Printing photo matrices (x,y,z) where z is 0:2 colour, I want to see (x in rows, y in columns) 3 times, once for each colour (the way I've been taught by every mathematician). By default, numpy does it last dimension first.

• As in print A.T? – Nat Wilson Mar 29 '13 at 23:46
• maybe this is a stackoverflow question...? – eusoubrasileiro Sep 5 '13 at 13:11

You can use array slicing.

So if you create a NumPy array (ndarray, to be precise), it might look like this:

a = numpy.arange(27).reshape(3,3,3)


And if you do print( a[:,:,:] ) it will print this out:

array([[[ 0,  1,  2],
[ 3,  4,  5],
[ 6,  7,  8]],

[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],

[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]])


And so you can do things like print( a[:,:,0] ) you get:

array([[ 0,  3,  6],
[ 9, 12, 15],
[18, 21, 24]])


Try experimenting with a[0,:,:] or a[:,0,:] to see if that's what you want.

Note that these are views on to the array, so if you want to store it somehow you should copy it into a new array.