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

Python 3.x 如何使用滑块回调来使用Python 3过滤Bokeh中的ColumnDataSource?,python-3.x,bokeh,Python 3.x,Bokeh,我正试图使用一个带有回调的滑块,在Bokeh中使用Python3来过滤ColumnDataSource对象的行(源于数据帧)。更具体地说,如果选项为0到10000000(100万的倍数)的滑块返回的值N为2000000,那么我希望我的绘图仅显示人口>=2000000的美国县的数据。下面是我的代码。除滑块回调外,所有操作都按我的要求进行 from bokeh.io import curdoc from bokeh.layouts import layout from bokeh.models im

我正试图使用一个带有回调的滑块,在Bokeh中使用Python3来过滤ColumnDataSource对象的行(源于数据帧)。更具体地说,如果选项为0到10000000(100万的倍数)的滑块返回的值
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编写另一个回调,并使绘图完全独立于服务器。

这里有一个解决方案,使用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中进行了测试

我使用了您的第一个解决方案,该解决方案运行良好。万分感谢!我对这个解决方案很感兴趣,但是在Bokeh2.2.3中我得到了以下错误。你有什么建议吗?:
错误:bokeh.core.validation.检查:E-1024(CDSVIEW_FILTERS_WITH_CONNECTED):CDSVIEW FILTERS与具有连接拓扑(如线或补丁)的图示符不兼容:
确定,切换到分散修复了图示符兼容性问题,bokeh 2中仍然没有MWE。