Я хотел выбрать и отменить выбор кривых одним щелчком мыши в боке Python - PullRequest
0 голосов
/ 24 января 2019

Я хотел выделить или отменить выбор всех кривых одним щелчком мыши в bokeh / Python3.x / jupyter, я создал кнопку выбора всех, чтобы выполнить эту работу одним щелчком мыши, но не знаю, как ее вызвать.

from bokeh.io import output_notebook, show
from bokeh.plotting import figure
from bokeh.palettes import Dark2_5 as palette
from bokeh.layouts import widgetbox, row, column
from bokeh.models import Button,CheckboxButtonGroup, CustomJS
import itertools
import numpy as np

# create a new plot (with a title) using figure
p = figure(plot_width=800, plot_height=400, title="My Line Plot")

start = 10.0
x = range(20)
colors = itertools.cycle(palette) 
nseries = 50

# add a line renderer
for n in range(nseries):
    y = np.cumsum(np.random.randn(1,20))
    p.line(x, y, line_width=1, legend=str(n), color=next(colors), name=str(n))

p.legend.location = "top_left"
p.legend.click_policy="hide"
select_all = Button(label="select all")
checkbox_button_group = CheckboxButtonGroup(
        labels=[str(n) for n in range(nseries)], active=[])

code = """
active = cb_obj.active;
rend_list = fig.renderers;
for (rend of rend_list) {
    if (rend.name!==null) {
        rend.visible = !active.includes(Number(rend.name));
    }
}
"""

checkbox_button_group.callback = CustomJS(args={'fig':p},code=code)

show(column([p, select_all,checkbox_button_group]))

1 Ответ

0 голосов
/ 29 января 2019

Одно из решений - сделать checkbox_button_group.active пустым и вызвать его обратный вызов.Добавьте следующие строки перед show...

select_all_code = """
checkbox_button_group.active=[]
callback.execute(checkbox_button_group, { fig })
"""
select_all.callback = CustomJS(args=dict(checkbox_button_group=checkbox_button_group,
                                        fig=p,
                                        callback=checkbox_button_group.callback), 
                                code=select_all_code)

Возможно, существует более элегантное решение.

...