custom color cycle from cmap

Hi,
I'm trying to plot a set of lines, 12 to be exact, and the default color
cycle only supports 8 or 9 distinct colors. That said, I looked up the color
maps and segmented it using 12 constant intervals with the hope of getting
12 distinct colors.

The problem I'm running in to is that some of the line colors I get are too
close to each other. This is because come shades in the colormap have a
broader spectrum than others.

Here is my code to set my custom default color cycle:

        import matplotlib as mpl
        cmap = mpl.cm.get_cmap(name='spectral') #I gues we can also use
"hsv" or "gist_rainbow"
        nColors = 12 #number of colors
        incr = 0.9 / nColors
        
        self.mycolors = []
        for i in np.arange(0,0.9,incr):
            self.mycolors.append(cmap(i))
        
        mpl.axes.set_default_color_cycle(self.mycolors)

Can anyone suggest a cleaner method? Or is there perhaps an existing class
to provide distinct color lines?

Thanks,
Krishna

···

--
View this message in context: http://old.nabble.com/custom-color-cycle-from-cmap-tp28177653p28177653.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

Hi,
I'm trying to plot a set of lines, 12 to be exact, and the default color
cycle only supports 8 or 9 distinct colors. That said, I looked up the color
maps and segmented it using 12 constant intervals with the hope of getting
12 distinct colors.

The problem I'm running in to is that some of the line colors I get are too
close to each other. This is because come shades in the colormap have a
broader spectrum than others.

Here is my code to set my custom default color cycle:

       import matplotlib as mpl
       cmap = mpl.cm.get_cmap(name='spectral') #I gues we can also use
"hsv" or "gist_rainbow"
       nColors = 12 #number of colors
       incr = 0.9 / nColors

       self.mycolors = []
       for i in np.arange(0,0.9,incr):
           self.mycolors.append(cmap(i))

you could replace the loop with a list comprehension:

mycolors = [cmap(i) for i in np.arange(0,0.9,incr)]

Also, arange may not be a great fit for this task; maybe linspace would work better:

mycolors = [cmap(i) for i in np.linspace(0, 0.9, nColors)]

This allows you to eliminate the assignment of `incr`. Note: the above colormap is different than that created by arange because linspace includes the endpoint, while arange doesn't.

Hope that helps,
-Tony

···

On Apr 8, 2010, at 8:13 AM, KrishnaPribadi wrote:

       mpl.axes.set_default_color_cycle(self.mycolors)

Can anyone suggest a cleaner method? Or is there perhaps an existing class
to provide distinct color lines?

Thanks,
Krishna

Thanks Tony. That helps clean up the code.

Now that I think about it more, I actaually had 2 questions.
The first you answered well.

The second question relates to my problem when using this method in that it
produces line colors where some colors are too similar. In other words,
there isn't enough of a stark differnece in color between the lines. Can
someone suggest a different method (or something that may already be built
in) of coming up with 12 or more line colors (more than the built in 8) that
are stark? (I know I'm nit-picking and can probably just pick out my own
colors but it's an interesting excersize).

···

--
View this message in context: http://old.nabble.com/custom-color-cycle-from-cmap-tp28177653p28180731.html
Sent from the matplotlib - users mailing list archive at Nabble.com.