When plotting with Bokeh, how do you automatically cycle through a color pallette?
It is probably easiest to just get the list of colors and cycle it yourself using itertools
:
import numpy as npfrom bokeh.plotting import figure, output_file, show# select a palettefrom bokeh.palettes import Dark2_5 as palette# itertools handles the cyclingimport itertools output_file('bokeh_cycle_colors.html')p = figure(width=400, height=400)x = np.linspace(0, 10)# create a color iteratorcolors = itertools.cycle(palette) for m, color in zip(range(10), colors): y = m * x p.line(x, y, legend='m = {}'.format(m), color=color)p.legend.location='top_left'show(p)
You can define a simple generator that cycles colors for you.
In python 3:
from bokeh.palettes import Category10import itertoolsdef color_gen(): yield from itertools.cycle(Category10[10])color = color_gen()
or in python 2 (or 3):
from bokeh.palettes import Category10import itertoolsdef color_gen(): for c in itertools.cycle(Category10[10]): yield ccolor = color_gen()
and when you need a new color, do:
p.line(x, y1, line_width=2, color=color)p.line(x, y2, line_width=2, color=color)
Here is the above example:
p = figure(width=400, height=400)x = np.linspace(0, 10)for m, c in zip(range(10), color): y = m * x p.line(x, y, legend='m = {}'.format(m), color=c)p.legend.location='top_left'show(p)