Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a color generator from given colormap in matplotlib

Tags:

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?

like image 850
Brendan Avatar asked Jun 10 '10 16:06

Brendan


People also ask

How do I generate a random color in Matplotlib?

Generating Random color Using Matplotlib LibraryAnd then using the join() function to join the # and color code. Color code will always start with #. Using for loop to iterate. Now the color code is generated.

What is Matplotlib colormap?

Scientifically, the human brain perceives various intuition based on the different colors they see. Matplotlib provides some nice colormaps you can use, such as Sequential colormaps, Diverging colormaps, Cyclic colormaps, and Qualitative colormaps.

How to create a custom color map using matplotlib?

Import a numpy, matplotlib library. From matplotlib importing cm and listedcolormap. Getting a named Colormap. The second argument is for the size of the list of colors. Creating a function named Colormap. Giving the size of the colormaps. Next, giving the size of the figure as 8,4. Create for loop. Plot the custom color map using matplotlib.

What is the default value of the colormap parameter n?

The parameter N denotes the number of entries in the colormap. It has a default value of None. When N has the value None, there should be one colormap entry for each color in colors list. To create a custom-listed colormap using the ListedColormap () method and a list of colors, you can pass names of four colors to the ListedColormap () method.

How to get the Max color of a series in Matplotlib?

from matplotlib import cm cmap = cm.get_cmap ('gist_rainbow') # ('hsv') # ('nipy_spectral') max_colors = 31 # Constant, max mumber of series in any plot.

How to create a custom listedcolormap in Python?

So, to create a custom ListedColormap in Python, we will have to create a list that will represent the colors of the colormap. We can create a custom-listed colormap using the ListedColormap () method. The syntax of the ListedColormap () method is as follows.


1 Answers

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)) 
like image 86
tom10 Avatar answered Oct 02 '22 18:10

tom10