When plotting with Bokeh, how do you automatically cycle through a color pallette?
<p>I want to use a loop to load and/or modify data and plot the result within the loop using Bokeh (I am familiar with <a href="http://matplotlib.org/1.2.1/examples/api/color_cycle.html" rel="nofollow">Matplotlib's <code>axes.color_cycle</code></a>).  Here is a simple example</p>

<pre><code>import numpy as np
from bokeh.plotting import figure, output_file, show
output_file('bokeh_cycle_colors.html')

p = figure(width=400, height=400)
x = np.linspace(0, 10)

for m in xrange(10):
    y = m * x
    p.line(x, y, legend='m = {}'.format(m))

p.legend.location='top_left'
show(p)
</code></pre>

<p>which generates this plot</p>

<p><img src="http://i.stack.imgur.com/9OtK9.png" alt="bokeh plot"></p>

<p>How do I make it so the colors cycle without coding up a list of colors and a modulus operation to repeat when the number of colors runs out?  </p>

<p>There was some discussion on GitHub related to this, issues <a href="https://github.com/bokeh/bokeh/issues/351" rel="nofollow">351</a> and <a href="https://github.com/bokeh/bokeh/issues/2201" rel="nofollow">2201</a>, but it is not clear how to make this work.  The four hits I got when searching the <a href="http://bokeh.pydata.org/en/latest/search.html?q=cycle+colors" rel="nofollow">documentation</a> for <code>cycle color</code> did not actually contain the word <code>cycle</code> anywhere on the page.</p>

<p>It is probably easiest to just get the list of colors and cycle it yourself using <a href="https://docs.python.org/3.5/library/itertools.html" rel="nofollow"><code>itertools</code></a>: </p>

<pre><code>import numpy as np
from bokeh.plotting import figure, output_file, show

# select a palette
from bokeh.palettes import Dark2_5 as palette
# itertools handles the cycling
import itertools  

output_file('bokeh_cycle_colors.html')

p = figure(width=400, height=400)
x = np.linspace(0, 10)

# create a color iterator
colors = itertools.cycle(palette)    

for m, color in itertools.izip(xrange(10), colors):
    y = m * x
    p.line(x, y, legend='m = {}'.format(m), color=color)

p.legend.location='top_left'
show(p)
</code></pre>

<p><a href="http://i.stack.imgur.com/L7l9G.png" rel="nofollow"><img src="http://i.stack.imgur.com/L7l9G.png" alt="enter image description here"></a></p>

<p>There does seem to be a <code>cycle_colors</code> function in <code>bokeh/charts/utils.py</code>, but it doesn't seem to be called anywhere else in the library, and it seems to be doing the exact same thing.</p>

<p>Here is an example demonstrating one of the many palettes available in <a href="https://stanford.edu/~mwaskom/software/seaborn/" rel="nofollow">seaborn</a> (they have anything you would expect from <code>matplotlib</code> and more):</p>

<pre><code>import numpy as np
from bokeh.plotting import figure, output_file, show
# seaborn handles the color palette generation, itertools handles the cycling
import seaborn.apionly as sns
import itertools 

output_file('bokeh_cycle_colors.html')

p = figure(width=400, height=400)
x = np.linspace(0, 10)

# define the color palette
ncolors = 5
palette = sns.palettes.color_palette('colorblind', ncolors)
# as hex is necessary for bokeh to render the colors properly.
colors = itertools.cycle(palette.as_hex())

for m, color in itertools.izip(xrange(10), colors):
    y = m * x
    p.line(x, y, legend='m = {}'.format(m), color=color)

p.legend.location='top_left'
show(p)
</code></pre>

<p><a href="http://i.stack.imgur.com/ugH9o.png" rel="nofollow"><img src="http://i.stack.imgur.com/ugH9o.png" alt="enter image description here"></a></p>

python plot bokeh