我正在使用ipython小部件来过滤pandas数据帧,并相应地更新一些Bokeh散点图的数据源。
代码运行正常并且没有错误,但仍然使用窗口小部件更改过滤参数值无效。
谁能告诉我问题出在哪里?谢谢!
TOOLS = "box_select,lasso_select,help,hover"
source = ColumnDataSource(data=frame[['A',
'B',
'C'
]])
left = figure(tools=TOOLS,
plot_width=300,
plot_height=300)
left.circle('A', 'C', source=source, size=10)
right = figure(tools=TOOLS,
plot_width=300,
plot_height=300,
title='A',
x_axis_label = "B",
y_axis_label = "C")
right.circle('B', 'C', source=source, size=10)
p = gridplot([[left, right]])
def update(tension):
df = frame[frame['D']==tension]
source = ColumnDataSource(data=df[['A',
'B',
'C'
]])
left.circle('A', 'C', source=source, size=10)
right.circle('B', 'C', source=source, size=10)
p = gridplot([[left, right]])
print(df.shape, tension)
push_notebook()
show(p, notebook_handle=True)
interact(update, tension=frame.D.unique())
使用小部件选择值确实会更改“张力”的值并更新“df”数据帧,但不会影响绘图。
答案 0 :(得分:1)
问题在于更新功能。检查此代码,这将有效。这会为D
列生成警告,但您可以忽略它们。
import pandas as pd
from bokeh.io import show,push_notebook,output_notebook
from bokeh.plotting import ColumnDataSource,gridplot,figure
from ipywidgets import interact
frame = pd.DataFrame({'A':[1,2,3,4,5,6,7,8,9,0],'B':[4,5,6,7,8,9,10,11,12,13],'C':[4,3,5,7,9,34,5,6,8,6],
'D':['Type A','Type B','Type C','Type A','Type B','Type C','Type A','Type B','Type C','Type A']})
output_notebook()
TOOLS = "box_select,lasso_select,help,hover"
source = ColumnDataSource(data=frame)
left_gp = figure(tools=TOOLS,plot_width=300, plot_height=300,x_axis_label = "A",y_axis_label = "C")
left = left_gp.circle('A', 'C', source=source, size=10)
right_gp = figure(tools=TOOLS, plot_width=300,plot_height=300,x_axis_label = "B",y_axis_label = "C")
right = right_gp.circle('B', 'C', source=source, size=10)
def update(tension):
left.data_source.data['A'] = frame[frame['D']==tension]['A']
left.data_source.data['C'] = frame[frame['D']==tension]['C']
right.data_source.data['B'] = frame[frame['D']==tension]['B']
right.data_source.data['C'] = frame[frame['D']==tension]['C']
push_notebook()
p = gridplot([[left_gp, right_gp]])
show(p, notebook_handle=True)
interact(update, tension=frame.D.unique())
请查看Bokeh doucuments中的official example。 对于该示例,输出看起来像这样。