Mapping color 2d array in matplotlib in Python

I would like to build a 2-dimensional matrix from numpy as a color matrix in Matplotlib. I have the following 9-by-9 array:

my_array = diag(ones(9)) # plot the array pcolor(my_array) 

I would like to establish that the first three elements of the diagonal are a specific color, the next three are a different color, and the last three are a different color. I would like to indicate the color by the hexadecimal code string, for example "# FF8C00". How can i do this?

Also, how can I set the color of 0-digit elements for pcolor?

+6
python numpy scipy matplotlib
source share
1 answer

To make the elements of different colors, assign them different values:

 my_array = diag([1,1,1,2,2,2,3,3,3]) 

To specify colors, try:

 from matplotlib.colors import ListedColormap, NoNorm cmap = ListedColormap(['#E0E0E0', '#FF8C00', '#8c00FF', '#00FF8C']) pcolor(my_array,cmap=cmap,norm=NoNorm()) 

The norm=NoNorm() argument avoids scaling the matrix values, so 0 gets the first color in the list, 1 gets the second color, etc.

+2
source share

All Articles