0

I have several numpy matrices collected over some time. I now want to visualize these matrices and explore visual similarities among them. The matrices contain small numbers from 0.0 to 1.0.

To compare them, I want to ensure that the same "areas" get colored with the same color, e.g. that 0.01 to 0.02 always is red, and 0.02 to 0.03 always is green. I have two question:

I found another question which has this code snippet:

a = np.random.normal(0.0,0.5,size=(5000,10))**2
a = a/np.sum(a,axis=1)[:,None]  # Normalize

plt.pcolor(a)

What is the effect of the second line, precisely the [:,None] statement. I tried normalizing a matrix by:

max_a = a/10# Normalize
print(max_a.shape)
plt.pcolor(max_a)

but there is not much visual difference compared to the visualization for the unnormalized matrix. When I then add the [:,None] statement I get an error

ValueError: too many values to unpack (expected 2)

which is expected since the shape now is 10,1,10. I therefor want to know what the brackets do and how to read the statement.

Secondly, and related, I want to make sure that I can visual compare the matrices. I therefor want to fix the "colorization", e.g. the ranges when a color is green or red, so that I do not end up with 0 to 0.1 as green in plot A and with 0 to 0.1 as red in plot B. How can I fix the "translation" from floats to colors? Do I have to normalize each matrix with a same constant, e.g. 10? Or do I normalize them with an unique value -- do I even need normalization here?

emil
  • 194
  • 1
  • 11

1 Answers1

1

[:,None] adds new axis so you'll be able to divide sum of all columns in each row - it is the same as using np.sum(a,axis=1)[:,np.newaxis] - when you sum all columns with np.sum(a,axis=1) you'll get 1d array with shape (5000), but to be able to normalize your matrix with summed columns you need 2d array with shape (5000,1), that's why new axis is needed.

You can have fixed colors by fixing scale of your colormap: plt.pcolor(max_a,vmin=0,vmax=1)

adding discrete colorbar might also help:

from pylab import cm

cmap = cm.get_cmap('jet', 10)  
plt.pcolor(a,cmap=cmap,vmin=0,vmax=1)
plt.colorbar()
YevKad
  • 650
  • 6
  • 13