如何使用滑块回调来使用Python 3过滤Bokeh中的ColumnDataSource?

时间:2017-09-26 07:30:07

标签: python-3.x bokeh

我正在尝试在Bokeh中使用带有回调的滑块,使用Python 3来过滤我的ColumnDataSource对象(源自DataFrame)的行。更具体地说,如果选项为0到10000000(以1百万的倍数)的滑块返回值为{200}的值N,那么我希望我的绘图仅显示数据,在本例中为美国县人口是> = 2000000.以下是我的代码。除了滑块回调之外,一切都按照我想要的方式工作。

from bokeh.io import curdoc
from bokeh.layouts import layout
from bokeh.models import HoverTool, ColumnDataSource, Select, Slider
from bokeh.plotting import figure

TOOLS='pan,wheel_zoom,box_zoom,reset,tap,save,box_select,lasso_select'

source1 = ColumnDataSource(df[df.winner == 'Democratic'])
source2 = ColumnDataSource(df[df.winner == 'Republican'])

hover = HoverTool(
        tooltips = [
            ('County Name', '@county'),
            ('Population', '@population'),
            ('Land Area', '@land_area'),
            ('Pop. Density', '@density'),
            ('Winning Party', '@winner'),
            ('Winning Vote %', '@winning_vote_pct'),
            ]
        )

# Plot
plot = figure(plot_width=800, plot_height=450, tools=[hover, TOOLS], 
           title='2016 US Presidential Vote % vs. Population Density (by County)',
           x_axis_label='Vote %', y_axis_label='Population Density (K / sq. mi.)')

y = 'density'
size = 'bokeh_size'
alpha = 0.5

c1 = plot.circle(x='pct_d', y=y, size=size, alpha=alpha, color='blue',
            legend='Democratic-Won County', source=source1)
c2 = plot.circle(x='pct_r', y=y, size=size, alpha=alpha, color='red',
            legend='Republican-Won County', source=source2)

plot.legend.location = 'top_left'

# Select widget
party_options = ['Show both parties', 'Democratic-won only', 'Republican-won only']
menu = Select(options=party_options, value='Show both parties')

# Slider widget
N = 2000000
slider = Slider(start=0, end=10000000, step=1000000, value=N, title='Population Cutoff')

# Select callback
def select_callback(attr, old, new):
    if menu.value == 'Democratic-won only': c1.visible=True; c2.visible=False
    elif menu.value == 'Republican-won only': c1.visible=False; c2.visible=True
    elif menu.value == 'Show both parties': c1.visible=True; c2.visible=True
menu.on_change('value', select_callback)

# Slider callback
def slider_callback(attr, old, new):
    N = slider.value
    # NEED HELP HERE...
    source1 = ColumnDataSource(df.loc[(df.winner == 'Democratic') & (df.population >= N)])
    source2 = ColumnDataSource(df.loc[(df.winner == 'Republican') & (df.population >= N)])
slider.on_change('value', slider_callback)

# Arrange plots and widgets in layouts
layout = layout([menu, slider],
                [plot])

curdoc().add_root(layout)

2 个答案:

答案 0 :(得分:2)

对代码进行最小更改的快速解决方案是:

def slider_callback(attr, old, new):
    N = new  # this works also with slider.value but new is more explicit
    new1 = ColumnDataSource(df.loc[(df.winner == 'Democratic') & (df.population >= N)])
    new2 = ColumnDataSource(df.loc[(df.winner == 'Republican') & (df.population >= N)])
    source1.data = new1.data
    source2.data = new2.data

更新数据源时,应该替换数据,而不是整个对象。在这里,我仍然创建新的ColumnDataSource作为快捷方式。更直接的方法(但也更详细)是从过滤后的df列创建字典:

    new1 = {
        'winner': filtered_df.winner.values,
        'pct_d': filtered_df.pct_d.values,
        ...
    }
    new2 = {...}
    source1.data = new1
    source2.data = new2

请注意,还有另一种解决方案可以使用CDSView CustomJSFilter来使回调本地化(而不是基于服务器)。您还可以使用CDSView编写其他回调,并使该图完全独立于服务器。

答案 1 :(得分:1)

这是一个使用CustomJSFilterCDSView的解决方案,这是Alex另一个答案中的建议。它不直接使用问题中提供的数据,而是一个一般性提示,说明如何实现:

from bokeh.layouts import column
from bokeh.models import CustomJS, ColumnDataSource, Slider, CustomJSFilter, CDSView
from bokeh.plotting import Figure, show
import numpy as np

# Create some data to display
x = np.arange(200)
y = np.random.random(size=200)

source = ColumnDataSource(data=dict(x=x, y=y))
plot = Figure(plot_width=400, plot_height=400)

# Create the slider that modifies the filtered indices
# I am just creating one that shows 0 to 100% of the existing data rows
slider = Slider(start=0., end=1., value=1., step=.01, title="Percentage")

# This callback is crucial, otherwise the filter will not be triggered when the slider changes
callback = CustomJS(args=dict(source=source), code="""
    source.change.emit();
""")
slider.js_on_change('value', callback)

# Define the custom filter to return the indices from 0 to the desired percentage of total data rows. You could also compare against values in source.data
js_filter = CustomJSFilter(args=dict(slider=slider, source=source), code=f"""
desiredElementCount = slider.value * 200;
return [...Array(desiredElementCount).keys()];
""")

# Use the filter in a view
view = CDSView(source=source, filters=[js_filter])
plot.line('x', 'y', source=source, line_width=3, line_alpha=0.6, view=view)

layout = column(slider, plot)

show(layout)

我希望这对以后偶然发现此问题的人有所帮助!在bokeh 1.0.2中进行了测试