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

How to use a slider callback to filter a ColumnDataSource in Bokeh using Python 3?

我正在尝试在 Bokeh 中使用带有回调的滑块,使用 Python 3 来过滤我的 ColumnDataSource 对象(源自 DataFrame)的行。更具体地说,如果一个滑块的选项为 0 到 10000000(100 万的倍数)returns 一个值 N 比如说 2000000,那么我希望我的绘图只显示数据,在这种情况下,美国人口 >= 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)

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

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 with a CustomJSFilter 使回调成为本地的(而不是基于服务器的)。您还可以使用 CDSView 编写另一个回调,并使绘图完全独立于服务器。

这是一个使用 CustomJSFilter and CDSView 的解决方案,正如 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)

我希望这对以后偶然发现这个的人有所帮助!在散景 1.0.2

中测试