cuxcrider
cuxcrider

Reputation: 125

Bokeh line plot color in ColumnDataSource

I would like to set the color of a Bokeh line plot (Bokeh version 0.12.5) using a ColumnDataSource. However, with a line plot nothing is plotted. On the other hand, if I use a circle renderer everything works as expected. Below is an example program with both a line plot and a circle plot and you can comment/uncomment the appropriate lines to see the plotting behavior. I also included a line of code for a line plot where the color is explicitly defined and the plot works perfectly. I have seen a couple similar questions asked but could not find a solid solution to this problem or determine if I am just doing something fundamentally wrong. Thanks for your help.

# bokeh version 0.12.5
# run in terminal with: python -m bokeh serve --show line_plot_color.py

from bokeh.io import curdoc
from bokeh.models import ColumnDataSource
from bokeh.plotting import Figure
from bokeh.layouts import row

source = ColumnDataSource(data = dict(color = ['green','green','green'], xs = [1,2,3], ys = [1,2,3]))
fig = Figure(plot_width=300, plot_height=300)

#r = fig.circle('xs','ys',source = source, size = 12, fill_color = 'color') # works as expected
r = fig.line('xs','ys',source = source, line_color = 'color') # fails to plot; no errors or warnings in terminal
#r = fig.line('xs','ys',source = source, line_color = 'green')  # works as expected

layout = row(fig)
curdoc().add_root(layout)

Upvotes: 3

Views: 8085

Answers (1)

DuCorey
DuCorey

Reputation: 895

First to help you with debugging the bokeh server, it's very useful to use the devtools that come with web browsers. The devtools' console will contain useful debugging information as is the case for your example.

Second, looking through the docs the line glyph method is not set up to receive a column data source value for its coloring. If you want to plot multiple lines with different colors on a single figure, then you can use the multi_line glyph. To use this glyph, you need to modify your data source xs and ys to be a list of list for each line in your multi_line. Here's a quick example.

source2 = ColumnDataSource(data = dict(color = ['green','red'], xs = [[1, 2],[2, 4]], ys = [[1, 2],[2, 4]]))
r = fig.multi_line('xs','ys',source = source2, line_color = 'color') 

Upvotes: 3

Related Questions