Creating a color generator from a given color map in matplotlib

I have a series of lines, each of which must be built with a separate color. Each line actually consists of several data sets (positive, negative areas, etc.), and therefore I would like to be able to create a generator that will give one color at a time over the spectrum, for example, the gist_rainbow map gist_rainbow shown here .

I found the following works, but it seems very complicated and, more importantly, it’s hard to remember,

 from pylab import * NUM_COLORS = 22 mp = cm.datad['gist_rainbow'] get_color = matplotlib.colors.LinearSegmentedColormap.from_list(mp, colors=['r', 'b'], N=NUM_COLORS) ... # Then in a for loop this_color = get_color(float(i)/NUM_COLORS) 

In addition, it does not cover the range of colors on the gist_rainbow map, I have to redefine the map.

Maybe the generator is not the best way to do this, if so, then what is accepted?

+11
python matplotlib color-mapping
Jun 10 '10 at 16:18
source share
1 answer

To index colors from a specific color map, you can use:

 import pylab NUM_COLORS = 22 cm = pylab.get_cmap('gist_rainbow') for i in range(NUM_COLORS): color = cm(1.*i/NUM_COLORS) # color will now be an RGBA tuple # or if you really want a generator: cgen = (cm(1.*i/NUM_COLORS) for i in range(NUM_COLORS)) 
+18
Jun 10 2018-10-1020:
source share



All Articles