user2041532
user2041532

Reputation: 1

how do i map a a vector of numbers to colors in python?

I'm having trouble finding a way to convert a vector of numbers to a "good" color scale (ala matplotlib) so that I can simply have just the vector of the color values. This is useful for rendering some static html pages in django. Any thoughts?

Thanks for the answers folks! Really helpful!

Upvotes: 0

Views: 920

Answers (2)

Francesco Montesano
Francesco Montesano

Reputation: 8658

you can also give a look at ColorConverter. It provides a few methods to convert valid matplotlib colors to RGB or RGBA.

Valid matplotlib colors are

  1. a letter from the set ‘rgbcmykw’
  2. a hex color string, like ‘#00FFFF’
  3. a standard name, like ‘aqua’ a float, like ‘0.4’, indicating gray on
  4. a 0-1 scale

Upvotes: 0

Developer
Developer

Reputation: 8400

You may use the following code:

from __future__ import division

import numpy
import matplotlib.cm as cmap

r = numpy.random.rand(1000)                             #random numbers
c = cmap.jet(numpy.int_((r-r.min())/r.ptp()*255))       #colors

so you try:

>>> r
array([ 0.88741281,  0.61022571,  0.14819983,  0.3695846 ,  0.73832029,
        0.6266069 ,  0.6917337 ,  0.09162752,  0.83532511,  0.54001574, ...,])
>>> c
array([[ 1.        ,  0.08787219,  0.        ,  1.        ],
       [ 0.83175206,  1.        ,  0.13598988,  1.        ],
       [ 0.        ,  0.08039216,  1.        ,  1.        ],
       ..., 
       [ 0.        ,  0.72352941,  1.        ,  1.        ],
       [ 0.24984187,  1.        ,  0.71790006,  1.        ],
       [ 1.        ,  0.45098039,  0.        ,  1.        ]])

which are colors each row in the format of RGBA.

Upvotes: 1

Related Questions