matplotlib: Get the colormap array

ahmadh picture ahmadh · Mar 3, 2012 · Viewed 9.6k times · Source

I am new to matplotlib, and have get stuck in colormaps.

In matplotlib how do I get the whole array of RGB colors for a specific colormap, let's say for "hot". For example if I was in MATLAB I would have just done this:

# in matlab
c = hot(256);
disp(c)

Any ideas?

Answer

Jouni K. Seppänen picture Jouni K. Seppänen · Mar 3, 2012

You can look up the values by calling the colormap as a function, and it accepts numpy arrays to query many values at once:

In [12]: from matplotlib import cm
In [13]: cm.hot(range(256))
Out[13]: 
array([[ 0.0416    ,  0.        ,  0.        ,  1.        ],
       [ 0.05189484,  0.        ,  0.        ,  1.        ],
       [ 0.06218969,  0.        ,  0.        ,  1.        ],
       ..., 
       [ 1.        ,  1.        ,  0.96911762,  1.        ],
       [ 1.        ,  1.        ,  0.98455881,  1.        ],
       [ 1.        ,  1.        ,  1.        ,  1.        ]])