使用 ipython 小部件更新散景散点图数据源
updating Bokeh scatter plots data source with ipython widgets
我正在使用 ipython 小部件来过滤 pandas 数据框,并相应地更新一些散景散点图的数据源。
代码运行正常,没有报错,但是用widget修改过滤参数值还是没有效果。
谁能告诉我问题出在哪里?谢谢!
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())
使用小部件选择一个值确实会更改 "tension" 的值并更新 "df" 数据框,但不会影响绘图。
问题出在更新功能上。检查此代码,这将起作用。这会为 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 文档中的 official example。
该示例的输出如下所示。
我正在使用 ipython 小部件来过滤 pandas 数据框,并相应地更新一些散景散点图的数据源。
代码运行正常,没有报错,但是用widget修改过滤参数值还是没有效果。
谁能告诉我问题出在哪里?谢谢!
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())
使用小部件选择一个值确实会更改 "tension" 的值并更新 "df" 数据框,但不会影响绘图。
问题出在更新功能上。检查此代码,这将起作用。这会为 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 文档中的 official example。
该示例的输出如下所示。