我想根据用户从下拉菜单中选择的内容更改简单线图的数据源。
我有 2 个数据框,我和我男朋友的体重和年龄。
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)
我将这两个数据框转换为 ColumnDataSource 对象,创建我的绘图和线条,添加我的下拉列表和 jslink。还有一个演示滑块显示如何更改线条的 line_width。
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)
当我运行这段代码时,我得到了错误
ValueError: expected an instance of type DataSource, got ColumnDataSource(id='5489', ...) of type str
从代码部分发生的错误dropdown2
。
是什么阻止了代码将 source 和 source2 识别为 ColumnDataSource() 对象?得到str 类型的 ColumnDataSource(id='5489', ...) 是什么意思?它是怎样的字符串?