使用选择工具基于列而不是行索引/索引进行散景链接/刷洗

问题描述 投票:0回答:2

问题涉及:Bokeh linking/ brushing based on column instead of row indices / index

我想使用选择工具进行多项选择而不是“点击”。为此,我使用customjs-for-selections显示的示例。不幸的是,我似乎陷入了使用source.selected.indices='new selection'的变化循环。任何建议如何更新选择而不触发js_on_change

from bokeh.plotting import figure, show
from bokeh.layouts import gridplot
from bokeh.models import ColumnDataSource, CDSView, BooleanFilter, CustomJS
import pandas as pd
output_notebook()

data = {'person': [1, 1, 1, 2, 2, 3, 3, 3, 3], 
        'activities':['a', 'b', 'c', 'a', 'b', 'a', 'b', 'c', 'd'], 
        'hours':[3, 4, 6, 2, 7, 5, 3, 2, 12], 
        'foodeaten':[12, 14, 34, 45, 67, 5, -1, 3, 5]}
df     = pd.DataFrame(data = data)
print(df)
source = ColumnDataSource(data = df)
views  = [(df.activities == l) for l in ['a', 'b', 'c', 'd']]
filtered_views = [CDSView(source = source, filters = [BooleanFilter(view.values.tolist())]) for view in views]
plot_options = dict(plot_width = 250, 
                    plot_height = 250, 
                    tools = "tap,pan,wheel_zoom,reset,save,box_select", 
                    tooltips = [("Person", "@person"), ("hours", "@hours")])
plots = [figure(title = 'activity {l}'.format(l = l), **plot_options) for l in ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']]
for plot, view, name in zip(plots, 2 * filtered_views, 4 * ['hours'] + 4 * ['foodeaten']):
    plot.circle('person', y = name, size = 15, view = view, source = source)

callback = CustomJS(args = dict(source = source, plots = plots), code = """
const selected_index = source.selected.indices[0]

const person = source.data['person'][selected_index]
var all_selected = [];
debugger;
for (index in source.data.index){
    if (source.data['person'][index] == person)
    all_selected.push(index)
}
source.selected.indices = all_selected
""")

callback1 = CustomJS(args = dict(source = source, ), code = """
var selected_indices = source.selected.indices;
var data = source.data;
debugger;
console.log(selected_indices)
var all_selected = [];
var persons = [];

for (i in selected_indices){
    index = selected_indices[i];
    console.log(data['person'][index]);
    persons.push(data['person'][index]);
    }


for (i in data.index){
    index = data.index[i]
    for (j in persons){
        person = persons[j]
        if (data['person'][i] == person){
            all_selected.push(index);
            }
    }
    }
source.selected.indices=all_selected;
""")


source.selected.js_on_change('indices', callback1)
output_file('testSelect.html')

show(gridplot(children = [plot for plot in plots], ncols = 2))
python plot bokeh
2个回答
1
投票

当TapTool替换为BoxSelectTool(在Bokeh v1.0.4上测试)时,前一个示例的回调仍然可以完成工作:

from bokeh.plotting import figure, show
from bokeh.layouts import gridplot
from bokeh.models import ColumnDataSource, CDSView, BooleanFilter, CustomJS, BoxSelectTool
import pandas as pd
# output_notebook()
data = {'person': [1, 1, 1, 2, 2, 3, 3, 3, 3],
        'activities':['a', 'b', 'c', 'a', 'b', 'a', 'b', 'c', 'd'],
        'hours':[3, 4, 6, 2, 7, 5, 3, 2, 12],
        'foodeaten':[12, 14, 34, 45, 67, 5, -1, 3, 5]}
df = pd.DataFrame(data = data)
print(df)
source = ColumnDataSource(data = df)
views = [(df.activities == l) for l in ['a', 'b', 'c', 'd']]
filtered_views = [CDSView(source = source, filters = [BooleanFilter(view.values.tolist())]) for view in views]
plot_options = dict(plot_width = 250,
                    plot_height = 250,
                    tools = "box_select,tap,pan,wheel_zoom,reset,save",
                    tooltips = [("Person", "@person"), ("hours", "@hours")])
plots = [figure(title = 'activity {l}'.format(l = l), **plot_options) for l in ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']]
for plot, view, name in zip(plots, 2 * filtered_views, 4 * ['hours'] + 4 * ['foodeaten']):
    plot.circle('person', y = name, size = 15, view = view, source = source)

callback = CustomJS(args = dict(source = source, plots = plots), code = """
const selected_index = source.selected.indices[0]
const person = source.data['person'][selected_index]
var all_selected = [];
//debugger;
for (index in source.data.index){
    if (source.data['person'][index] == person)
    all_selected.push(index); }
source.selected.indices = all_selected; """)

# output_file('testSelect.html')
for plot in plots:
    plot.select_one(BoxSelectTool).callback = callback
show(gridplot(children = [plot for plot in plots], ncols = 2))

0
投票

根据Tony的回答(使用BoxSelectTool进行单选),我想出了多选的代码。 (注意我在原来的callback1函数中输入了一些错字)

from bokeh.plotting import figure, show
from bokeh.layouts import gridplot
from bokeh.models import ColumnDataSource, CDSView, BooleanFilter, CustomJS, BoxSelectTool
import pandas as pd
# output_notebook()
data = {'person': [1, 1, 1, 2, 2, 3, 3, 3, 3],
        'activities':['a', 'b', 'c', 'a', 'b', 'a', 'b', 'c', 'd'],
        'hours':[3, 4, 6, 2, 7, 5, 3, 2, 12],
        'foodeaten':[12, 14, 34, 45, 67, 5, -1, 3, 5]}
df = pd.DataFrame(data = data)
print(df)
source = ColumnDataSource(data = df)
views = [(df.activities == l) for l in ['a', 'b', 'c', 'd']]
filtered_views = [CDSView(source = source, filters = [BooleanFilter(view.values.tolist())]) for view in views]
plot_options = dict(plot_width = 250,
                    plot_height = 250,
                    tools = "box_select,tap,pan,wheel_zoom,reset,save",
                    tooltips = [("Person", "@person"), ("hours", "@hours")])
plots = [figure(title = 'activity {l}'.format(l = l), **plot_options) for l in ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']]
for plot, view, name in zip(plots, 2 * filtered_views, 4 * ['hours'] + 4 * ['foodeaten']):
    plot.circle('person', y = name, size = 15, view = view, source = source)


callback1 = CustomJS(args = dict(source = source, ), code = """
var selected_indices = source.selected.indices;
var data = source.data;
debugger;
console.log(selected_indices)
var all_selected = [];
var persons = [];

for (i in selected_indices){
    index = selected_indices[i];
    console.log(data['person'][index]);
    persons.push(data['person'][index]);
    }


for (i in data.index){
    index = data.index[i]
    for (j in persons){
        person = persons[j]
        if (data['person'][i] == person){
            all_selected.push(index);
            }
    }
    }
source.selected.indices=all_selected;
""")

# output_file('testSelect.html')
for plot in plots:
    plot.select(BoxSelectTool).callback = callback1
show(gridplot(children = [plot for plot in plots], ncols = 2))
© www.soinside.com 2019 - 2024. All rights reserved.