22

I have a series of lines that each need to be plotted with a separate colour. Each line is actually made up of several data sets (positive, negative regions etc.) and so I'd like to be able to create a generator that will feed one colour at a time across a spectrum, for example the gist_rainbow map shown here.

I have found the following works but it seems very complicated and more importantly difficult 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)

Moreover, it does not cover the range of colours in the gist_rainbow map, I have to redefine a map.

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

smci
  • 32,567
  • 20
  • 113
  • 146
Brendan
  • 18,771
  • 17
  • 83
  • 114

1 Answers1

37

To index colors from a specific colormap 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))
Community
  • 1
  • 1
tom10
  • 67,082
  • 10
  • 127
  • 137
  • :Hello,i wanted to ask you how can i use this in my program.I have for example cells which have integer values(empty=0,full=1..) .How can i make that "empty" corresponds to color 'red',full to color white etc.I have a function in which i make the plot "....im=plt.imshow(mydata,cmap=plt.get_cmap('gist_earth'))" .How must i implement the above?(if we say the same thing).Thanks! – George Jan 24 '12 at 15:47
  • @George: As you describe it, I'm not sure why this doesn't work for you. Maybe post a full question with an small example. – tom10 Jan 25 '12 at 18:04
  • :If you could check here http://stackoverflow.com/questions/8929456/how-to-create-a-movie-in-relation-to-matlab (at the updated part )where i create the graph.How can i implement your example?(If you insist i will post a new answer) Thanks! – George Jan 25 '12 at 18:09
  • Maybe a new post.. I'm really not getting it. I wonder though, maybe you're wanting to use vmin and vmax to keep your colorbar range fixed between different plots? For example, see my answer here: http://stackoverflow.com/questions/3373256/set-colorbar-range-in-matplotlib – tom10 Jan 25 '12 at 18:22
  • :Ok, i'll check it tomorrow.Maybe it's that.Else,i will make a new post and let you know.Thanks! – George Jan 25 '12 at 18:29
  • If the color map ranges from 0 to 1, but i goes from 0 to `NUM_COLORS-1`, then don't you miss the color of the map that corresponds to 1? – esmit Jun 20 '13 at 06:05
  • @esmit: yes, good point. I was just sticking with the same colors the OP used. Some colormaps end on a similar color as they start, so in this case it might be best not to end on 1, but if you wanted to, of course, you could use NUM_COLORS+1 or linspace. – tom10 Jun 20 '13 at 15:05