Numpy and Matplotlib actually represents 3 dimension in x,y,z format. Whereas pytorch represents them as z,x,y. So with (np.transpose(npimg, (1, 2, 0)) we are telling numpy the mapping of the indexed. i.e.
for a torch Image matrix with shape (3,32,32) where 3 is z (channel) 32 is x and 32 is y  (np.transpose(npimg, (1, 2, 0)) says transpose the given matrix npimg into numpy where value of x,y.z are at index 1,2,0 respectively.