Create a color generator from given colormap in matplotlib
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))
Brendan
Was a physicist, now a freelance technologist and UX designer Most at home with C#, XAML, Javascript and Python with experience in lots of other languages and technologies.
Updated on July 09, 2022Comments
-
Brendan almost 2 years
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?
-
George over 12 years: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!
-
tom10 over 12 years@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.
-
George over 12 years:If you could check here stackoverflow.com/questions/8929456/… (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!
-
tom10 over 12 yearsMaybe 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: stackoverflow.com/questions/3373256/…
-
George over 12 years:Ok, i'll check it tomorrow.Maybe it's that.Else,i will make a new post and let you know.Thanks!
-
esmit almost 11 yearsIf 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? -
tom10 almost 11 years@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.