I’m trying to plot multiple lines (mutli_line) depending on what the user selects (MultiSelect). When I set the initialized values in MutlitSelect I get the proper lines; however, when I try to select more than one value in the MultiSelect widget the plot breaks. The weird thing is that I can select one value in the MultiSelect widget the plot with work. Here is an example piece of code which illustrates this:
import pandas as pd
import numpy as np
import datetime as dt
from bokeh.models import (HoverTool, PanTool, WheelZoomTool,
BoxZoomTool, ResetTool, SaveTool)
from bokeh.palettes import RdYlBu11
from bokeh.plotting import figure, show, output_file, ColumnDataSource
from bokeh.io import curdoc
from bokeh.layouts import row, column
from bokeh.models.widgets import MultiSelect
def create_ts(n_pnts=100):
return np.sin(np.linspace(0, 2, n_pnts)) + np.random.normal(0, .2, n_pnts)
def create_dates(start_date, n_dates):
return [start_date + dt.timedelta(days=i) for i in range(n_dates)]
ts_dict = {‘ts1’:create_ts(100), ‘ts2’:create_ts(100), ‘ts3’:create_ts(100),
‘ts4’:create_ts(100), ‘ts5’:create_ts(100)}
dates = create_dates(dt.date(2016,01,01), 100)
source_ts = ColumnDataSource(data=dict(xs=, ys=, color=))
-------------
Setup widgets
-------------
ms_loc = MultiSelect(
title=‘Market Location’,
value=[ts_dict.keys()[0],ts_dict.keys()[2]],
options=ts_dict.keys(),
width=300)
----------------------------------
Set county color depending on what
is selected in MultiSelect
----------------------------------
def set_color(n_selected):
colors_used =
for i in range(n_selected):
l = i - (i/len(RdYlBu11))*len(RdYlBu11)
colors_used.append(RdYlBu11[l])
return colors_used
---------------------
Set up maps and plots
---------------------
TOOLS = “pan,wheel_zoom,box_zoom,reset,hover,save”
ts = figure(plot_width=1000, plot_height=700, tools=TOOLS,
x_axis_type=‘datetime’)
ts.multi_line(‘xs’, ‘ys’, source=source_ts, color=‘color’)
ts.yaxis.axis_label = ‘Monthly Averaged Market Price’
ts.xaxis.axis_label = ‘Date’
---------
Callbacks
---------
def select_change(attrname, old, new):
update()
def update():
ts_list = ms_loc.value
color_used = set_color(len(ts_list))
ts_dates = [dates for t in range(len(ts_list))]
vals =
for k in ts_list:
vals.append(list(ts_dict[k]))
source_ts.data[‘xs’] = ts_dates
source_ts.data[‘ys’] = vals
source_ts.data[‘color’] = color_used
ms_loc.on_change(‘value’, select_change)
---------------
Arrange widgets
---------------
layout = column(ms_loc, ts)
-----------------
Initialize graph
-----------------
update()
curdoc().add_root(layout)
curdoc().title = “test”
``
I’m using Bokeh version 0.12.1.
Any help is greatly appreciated.
Thanks.