散景:滑块不会更新Hbar图上的结果

时间:2020-10-23 22:26:25

标签: widget slider bokeh

我编写了以下代码,使用滑块来过滤和更新Bokeh中Hbar图上的值。 绘图(如图所示)可以正确输出,但是当我移动滑块时什么也没有发生。 非常感谢您的反馈。

import pandas as pd
from bokeh.core.properties import value
from IPython.display import display, HTML
from bokeh.plotting import figure, show
from bokeh.layouts import row, column, gridplot
from bokeh.io import output_notebook, save, curdoc
from bokeh.models import ColumnDataSource, HoverTool, DatetimeTickFormatter, FactorRange, DataTable, TableColumn, DateFormatter
from bokeh.models.widgets import Panel, Tabs, Slider
import matplotlib.pyplot as plt

xls=pd.ExcelFile(path)
test_data=pd.read_excel(xls, 'test_data')

display(test_data)

AREA    counts
A   500
B   100
C   70
D   50
E   40
F   20
G   10
H   2

def myplot(doc):

    source = ColumnDataSource(pd.DataFrame(data=test_data))
    area_list=source.data['AREA'].tolist()[::-1]

# Creating the Bar Chart

    p = figure(y_range=area_list ,plot_height=500, plot_width=500, title="Total counts per area",
           x_axis_label='counts', y_axis_label='AREA')
           

    p.hbar(y='AREA', right='counts',  height=1, 
            line_color="black", fill_color='red',line_width=1, 
            source=source)

    def update_plot(attr, old, new):

        Number_of_counts = slider.value
        new_data = test_data.loc[test_data['counts'] >=Number_of_counts]
        source = ColumnDataSource(data=new_data)


# Make a slider object: slider
    slider = Slider(start=1, end=100, step=1, value=1, title='counts')

# Attach the callback to the 'value' property of slider
    slider.on_change('value', update_plot)
 
    doc.add_root(column(slider, p))


show(myplot)

enter image description here

1 个答案:

答案 0 :(得分:1)

您要替换source变量的值,但是旧的源仍然存在,所有创建的模型都在使用它。

而不是重新创建源,请尝试重新分配旧源的data属性:

# source = ColumnDataSource(data=new_data)
source.data = ColumnDataSource.from_df(new_data)