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?
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.
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.
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.
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.
from matplotlib import cm cmap = cm.get_cmap ('gist_rainbow') # ('hsv') # ('nipy_spectral') max_colors = 31 # Constant, max mumber of series in any plot.
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.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With