Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When plotting with Bokeh, how do you automatically cycle through a color pallette?

Tags:

python

plot

bokeh

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 Matplotlib's axes.color_cycle). Here is a simple example

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) 

which generates this plot

bokeh plot

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?

There was some discussion on GitHub related to this, issues 351 and 2201, but it is not clear how to make this work. The four hits I got when searching the documentation for cycle color did not actually contain the word cycle anywhere on the page.

like image 585
Steven C. Howell Avatar asked Oct 03 '16 19:10

Steven C. Howell


People also ask

How do you use Bokeh plots?

plotting : A high level interface for creating visual glyphs. The dataset used for generating bokeh graphs is collected from Kaggle. To create scatter circle markers, circle() method is used. To create a single line, line() method is used.

How do I get rid of gridlines in bokeh?

You can hide the lines by setting their grid_line_color to None .


1 Answers

It is probably easiest to just get the list of colors and cycle it yourself using itertools:

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 zip(range(10), colors):     y = m * x     p.line(x, y, legend='m = {}'.format(m), color=color)  p.legend.location='top_left' show(p) 

enter image description here

like image 51
Elliot Avatar answered Oct 16 '22 14:10

Elliot