Python Bokeh-使用选择小部件和交互式图例的交互式绘图-空html文件
我提出了一些与博克密谋的问题。“类别”上有一个过滤器,因此绘图应在更改时更新。此外,该情节还有一个互动图例 密码一定有问题。我在jupyter笔记本中得到一个错误的错误列名称,但可能也有问题,html文件是空的。求你了,我需要一些帮助。谢谢大家!Python Bokeh-使用选择小部件和交互式图例的交互式绘图-空html文件,python,plot,filter,bokeh,Python,Plot,Filter,Bokeh,我提出了一些与博克密谋的问题。“类别”上有一个过滤器,因此绘图应在更改时更新。此外,该情节还有一个互动图例 密码一定有问题。我在jupyter笔记本中得到一个错误的错误列名称,但可能也有问题,html文件是空的。求你了,我需要一些帮助。谢谢大家! # Perform necessary imports import pandas as pd from bokeh.io import output_file, show from bokeh.plotting import figure from
# Perform necessary imports
import pandas as pd
from bokeh.io import output_file, show
from bokeh.plotting import figure
from bokeh.models import CustomJS, HoverTool, ColumnDataSource, Select
from bokeh.palettes import Category20_20
from bokeh.layouts import layout, widgetbox, gridplot
#Dataframe
df = pd.DataFrame({'Reg': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'],
'Category': ['Cat1', 'Cat1', 'Cat1', 'Cat2', 'Cat2', 'Cat2', 'Cat3', 'Cat3', 'Cat3', 'Cat3'],
'X': ['751', '673', '542', '762', '624', '536', '845', '626', '876', '233'],
'Y': ['458', '316', '287', '303', '297', '564', '278', '834', '234', '623'],
'Size': ['5', '9', '5', '8', '10', '22', '23', '12', '9', '20'],
'User': ['u1', 'u2', 'u3', 'u1', 'u2', 'u3', 'u1', 'u2', 'u3', 'u3']
})
# Make the ColumnDataSource
source = ColumnDataSource(data=dict(Reg=df['Reg'], Category=df['Category'], x=df['X'], y=df['Y'], Size=df['Size'], User=df['User']))
filteredSource = ColumnDataSource(data=dict(Reg=[], Category=[], x=[], y=[], Size=[], User=[]))
#Create widget
category = list(set(df['Category']))
category.sort()
category_select = Select(title="Category:", value=category[0], options=category)
#Callback
callback = CustomJS(args=dict(source=source, filteredSource=filteredSource,
category_select=category_select), code="""
const data = source.data;
var f = cb_obj.value;
const df2 = filteredSource.data;
df2['category']=[]
for(i = 0; i < data['category'].length;i++){
if(data['category'][i]==f){
df2['category'].push(data['category'][i])
}
}
filteredSource.change.emit()
""")
category_select.js_on_change('value', callback)
# Create the figure: p1
p1 = figure(x_axis_label='x)', y_axis_label='y',
plot_width=450, plot_height=450, tools=['box_select', HoverTool(tooltips='Size: @size')])
# Add a circle glyph to p1
users=list(set(df['User']))
for name, color in zip(users, Category20_20):
user_df = df[df['User'] == name]
p1.circle(x='X', y='Y', size='Size',
color=color, alpha=0.8, source=filteredSource, legend=name)
p1.legend.location = "top_right"
p1.legend.click_policy="hide"
#layout
layout = gridplot([widgetbox(category_select), p1], ncols=2, sizing_mode='scale_both')
show(layout)
列名中的字符大小写不一致。例如,使用x和x。您必须坚持使用一个变量,并在任何地方使用它-在数据源中,在创建glyph渲染器时在列规范中,在CustomJS代码中,等等 另外,您的CustomJS代码是错误的。i=0将导致错误-您应该改用let i=0。您还只更新Category列,而您必须更新所有列,以确保它们的长度相同
作为旁注,从这个问题和另一个问题来看,您可能会发现此文档部分非常有用:。这里描述的方法有助于防止编写JS代码和不必要的数据搅动。这里和博克的论述中都有许多例子。谢谢你的反馈,尤金!