【问题标题】:Bokeh source change散景源更改
【发布时间】:2019-04-15 15:25:36
【问题描述】:

我正在尝试使用函数更新散景散点图的数据源。 但是,该图不仅显示了新数据,还显示了所有数据。

我想我正在向绘图传递一个新的数据源,但旧的绘图点仍然存在。

您将如何仅使用新数据更新散点图?

另外,有没有什么方法可以在不与之交互的情况下检索下拉菜单中的当前选择? (即没有使用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'}

【问题讨论】:

    标签: bokeh


    【解决方案1】:

    我在您的代码中更改了一些内容,如果您在下拉菜单中选择空值或在下拉菜单中选择其他值之一时选择随机生成的数据集,它现在会显示您的原始数据。使用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'}
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多