我正在尝试使用函数更新Bokeh散点图的数据源。但是,该图不是仅绘制新数据,而是显示所有数据。
我想我正在将新的数据源传递给情节,但旧的绘图点仍然存在。
如何使用新数据更新散点图?
此外,有没有办法在下拉菜单中检索当前选择而不与它交互? (即没有使用on_change
的回调)
import numpy as np
import pandas as pd
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import Tabs, Select
from bokeh.layouts import column, row, Spacer
from bokeh.io import curdoc
from bokeh.plotting import figure, curdoc, show
#Plotting points on initial chart.
df_AB = pd.DataFrame(np.random.randint(0,100,size=(500, 2)), columns=list('AB'), index=[str(i) for i in range(1,500+1)])
pointchart=figure(plot_width=800, plot_height=700, tools=['lasso_select','box_select'],title="Point scatter")
pointchart_source= ColumnDataSource(df_AB[["A","B"]])
pointchart_glyph= pointchart.circle("A","B",source=pointchart_source)
#Dropdown
selectoroptions=['','new selection', 'other selection']
Xselector = Select(title="Dropdown:", value="", options=selectoroptions)
#Callback to update data source
def Xdropdownchange(attrname, old, new):
pointchart_glyph= pointchart.circle("X","Y",source=make_updated_source())
Xselector.on_change("value", Xdropdownchange)
#Making new/updated data source based on dropdowns.
df_XY = pd.DataFrame(np.random.randint(0,100,size=(500, 2)), columns=list('XY'), index=[str(i) for i in range(1,500+1)])
def make_updated_source():
new_x=pd.Series(list(df_XY.iloc[0:100]["X"]),name="X")
new_y=pd.Series(list(df_XY.iloc[0:100]["Y"]),name="Y")
sourcedf=pd.DataFrame([new_x,new_y]).T
pointchart_source= ColumnDataSource(sourcedf)
return pointchart_source
#Show
layout=row(column(Xselector, Spacer(width=400, height=500)),pointchart)
curdoc().add_root(layout)
!powershell -command {'bokeh serve --show Dropdown_sourcechange.ipynb'}
我在代码中更改了一些内容,如果您在下拉列表中选择空值或者在下拉列表中选择其他值之一时随机生成的数据集中显示原始数据。使用print(Xselector.value)
也可以在不使用回调的情况下检索下拉列表中的当前选择
import numpy as np
import pandas as pd
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import Tabs, Select
from bokeh.layouts import column, row, Spacer
from bokeh.io import curdoc
from bokeh.plotting import figure, curdoc, show
#Plotting points on initial chart.
df_AB = pd.DataFrame(np.random.randint(0,100,size=(500, 2)), columns=list('XY'), index=[str(i) for i in range(1,500+1)])
pointchart=figure(plot_width=800, plot_height=700, tools=['lasso_select','box_select','wheel_zoom'],title="Point scatter")
source= ColumnDataSource(df_AB[["X","Y"]])
pointchart.circle("X","Y",source=source)
#Dropdown
selectoroptions=['','new selection', 'other selection']
Xselector = Select(title="Dropdown:", value="", options=selectoroptions)
def make_updated_source(attr, old, new):
if new == '':
source.data = ColumnDataSource(df_AB[["X","Y"]]).data
else:
df_XY = pd.DataFrame(np.random.randint(0,100,size=(500, 2)), columns=list('XY'), index=[str(i) for i in range(1,500+1)])
new_x=pd.Series(list(df_XY.iloc[0:100]["X"]),name="X")
new_y=pd.Series(list(df_XY.iloc[0:100]["Y"]),name="Y")
sourcedf=pd.DataFrame([new_x,new_y]).T
source.data = ColumnDataSource(sourcedf).data
Xselector.on_change("value", make_updated_source)
#Retrieve selection in dropdown withoud on_change
print(Xselector.value)
#Show
layout=row(column(Xselector, Spacer(width=400, height=500)),pointchart)
curdoc().add_root(layout)
!powershell -command {'bokeh serve --show Dropdown_sourcechange.ipynb'}