I want to change the data source of a simple line plot depending on what the user picks from a dropdown menu.
I have 2 dataframes, the weight and age of myself and my boyfriend.
my_weight = [60,65,70]
my_age = [21,22,25]
d_weight = [65,70,80]
d_age = [21,22,25]
me = pd.DataFrame(list(zip(my_weight, my_age)),
columns =['weight', 'age'], index=None)
dillon = pd.DataFrame(list(zip(d_weight, d_age)),
columns =['weight', 'age'], index=None)
I turn these two dataframe into ColumnDataSource objects, create my plot and line, add my dropdown and jslink. There is also a demo slider to show how I can change the line_width of my line.
from bokeh.models import ColumnDataSource
from bokeh.core.properties import Any, Bool, ColumnData
pn.extension()
source = ColumnDataSource(me, name="Me")
source2 = ColumnDataSource(dillon, name="Dillon")
# print("Me: ", source.data, "Dillon: ", source2.data)
plot = figure(width=300, height=300)
myline = plot.line(x='weight', y='age', source=source, color="pink")
width_slider = pn.widgets.FloatSlider(name='Line Width', start=0.1, end=10)
width_slider.jslink(myline.glyph, value='line_width')
dropdown2 = pn.widgets.Select(name='Data', options=[source, source2])
dropdown2.jslink(myline, value='data_source')
pn.Column(dropdown2, width_slider, plot)
When I run this code, I get the error
ValueError: expected an instance of type DataSource, got ColumnDataSource(id='5489', ...) of type str
with the error occurring from the dropdown2
section of code.
Whats preventing the code from recognizing source and source2 as ColumnDataSource() objects? What is meant by got ColumnDataSource(id='5489', ...) of type str? How is it a string?