Python Bokeh:使用复选框小部件隐藏和显示绘图

Python Bokeh:使用复选框小部件隐藏和显示绘图,python,widget,bokeh,Python,Widget,Bokeh,我在使用Bokeh的小部件回调时遇到了一些困难。在复选框小部件的帮助下,我想显示/隐藏相应的绘图 与的区别是,我希望每个图示符都有一个绘图(而不是同一绘图上的所有图示符)。例如,如果我勾选/取消勾选“b”,我希望看到图示符的新绘图 编辑:我的代码的新版本 from bokeh.io import output_file, show from bokeh.layouts import column, widgetbox, row from bokeh.models impo

我在使用Bokeh的小部件回调时遇到了一些困难。在复选框小部件的帮助下,我想显示/隐藏相应的绘图

与的区别是,我希望每个图示符都有一个绘图(而不是同一绘图上的所有图示符)。例如,如果我勾选/取消勾选“b”,我希望看到图示符的新绘图

编辑:我的代码的新版本

    from bokeh.io import output_file, show
    from bokeh.layouts import column, widgetbox, row
    from bokeh.models import ColumnDataSource
    from bokeh.plotting import figure
    from bokeh.models.widgets import CheckboxGroup
    from bokeh.models import CustomJS
    import pandas as pd
    import numpy as np

    if __name__ == '__main__':

        # Plot tools
        TOOLS = 'box_select,box_zoom,reset'

        # Data Source
    df = pd.DataFrame(np.random.randn(10, 5), columns=['a', 'b', 'c', 'd', 'e'])
    source = ColumnDataSource(df)

    # Create a new glyph and share ranges
    g = figure(plot_width=1300, plot_height=150, title='a', tools=TOOLS)
    g.circle(source=source, x='a', y='a')

    glyph_list = []

    for glyph in range(0, len(source.column_names)):
        glyph_list.append((figure(plot_width=1300, plot_height=150, x_range=g.x_range, title=source.column_names[glyph],
                                 tools=TOOLS)))

        glyph_list[glyph].circle(source=source, x='a', y=source.column_names[glyph])

    # Select the glyphs to plot
    initial_list = [0] * len(source.column_names)

    ## example of a change
    initial_list[2] = 1

    # Callback
    code = """
            ????
          """

    callback = CustomJS(args=dict(source=source), code=code) #????

    # Add checkbox widget
    checkbox_group = CheckboxGroup(labels=source.column_names,
                                   callback=callback,
                                   active=initial_list)

    plot_list = []
    for i in range(0, len(source.column_names)):
        if checkbox_group.active[i] == 1:
            plot_list.append(glyph_list[i])

    checkbox_group.js_on_change('active', callback) # ???


    layout = row(column(plot_list), widgetbox(checkbox_group))
    show(layout)

使用Python on_click处理程序而不是CustomJS回调更容易实现:

from bokeh.layouts import column
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import CheckboxGroup
from bokeh.plotting import curdoc, figure
from bokeh.client import push_session
import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(10, 4), columns = ['a', 'b', 'c', 'd'])
source = ColumnDataSource(df)

def checkbox_click_handler(selected_checkboxes):
    visible_glyphs = layout.children
    for index, glyph in enumerate(glyph_list):
        if index in selected_checkboxes:
            if glyph not in visible_glyphs:
                layout.children.append(glyph)
        else:
            if glyph in visible_glyphs:
                layout.children.remove(glyph)

checkbox_group = CheckboxGroup(labels = list(df.columns.values), active = [0, 1, 2, 3, 4])
checkbox_group.on_click(checkbox_click_handler)

layout = column()
layout.children.append(checkbox_group)

glyph_list = []
for index, letter in enumerate(df.columns.values):
    glyph = figure(plot_width = 800, plot_height = 240, title = letter, name = letter)
    glyph.circle(source = source, x = 'a', y = letter)
    glyph_list.append(glyph)
    layout.children.append(glyph)

session = push_session(document = curdoc())
session.show(layout)
session.loop_until_closed()

请详细说明“…每个数据的一个绘图(而不是同一绘图上的所有行)…”。您的意思是使不同轴上/下可见吗?那些轴在网格中?它们是否位于尺寸为:行x1的列中?或网格:行x列?无法推送会话文档,因为无法连接到服务器(若要启动服务器,请尝试“bokeh serve”命令):运行-session=push_会话(document=curdoc())时:::执行以下操作::导入操作系统;操作系统(“bokeh服务”)打开命令行(Windows)或终端(Mac),键入“bokeh服务”启动bokeh服务器