如何为散景网格图设置默认/活动工具?

Dre*_*met 6 python plot python-3.x bokeh

如果想要定义散景图的活动(默认)工具,可以通过将“active_drag”、“active_inspect”等参数传递给图形实例来设置,如此处所述

我尚未成功为网格图设置标准活动工具,其中所有单个图共享一个工具栏。这是我的代码的相关部分:

    tools = [
            PanTool(),
            BoxZoomTool(),
            WheelZoomTool(),
            UndoTool(),
            RedoTool(),
            ResetTool(),
            SaveTool(),
            HoverTool(tooltips=[
                ("Value", "$y")
                ])
            ]

    x_axes_range = Range1d(self.data.index[0], self.data.index[-1])

    for plot_type, plot_settings in pcfg.plot_types[self.name].items():

        plots.append(figure(x_axis_type="datetime", title=plot_type, plot_height = 400, x_range = x_axes_range, 
                            tools = tools, active_drag = None, active_inspect = None, active_scroll = None, active_tap = None))
    ...

    plots[plot_counter].line(self.data.index, self.data[parameter], color=parameter_settings[1], legend=parameter_settings[0])

...

gp = gridplot(plots, ncols = 1, sizing_mode = "scale_width")

script, div = components(gp)
Run Code Online (Sandbox Code Playgroud)

因此,发生的情况是,“BoxZoomTool()”在我显示它的网站上被选为活动工具,尽管我在图形初始化中将活动工具设置为 None,但可用工具是我传递给 inits 的工具figure()

我确实在这里看到了“toolbar_option” ,但我不知道如何使用该参数更改活动工具。

Che*_*uCR 6

更新 2020 年 6 月 4 日

\n

嗯,似乎有人创建了一个GitHub Issue并且更改已经被合并了。让我们看看这是否适用于下一个 Bokeh 版本

\n
\n

旧答案

\n

研究

\n

toolbar_options我相信你只能这样改变标志:

\n
toolbar_options=dict(logo=\'gray\')\n
Run Code Online (Sandbox Code Playgroud)\n

我希望未来有更多的选择。

\n

我已经检查了如何实现你想要的(我也需要这样做),似乎网格图使用一个特殊的工具栏将所有绘图工具栏连接在一起:一个ProxyToolbar

\n
# Make the grid\ntools = []\nrows = []\n\nfor row in children:\n    row_tools = []\n    row_children = []\n    for item in row:\n        if merge_tools:\n            if item is not None:\n                for plot in item.select(dict(type=Plot)):\n                    row_tools = row_tools + plot.toolbar.tools\n                    plot.toolbar_location = None\n        if item is None:\n            width, height = 0, 0\n            for neighbor in row:\n                if isinstance(neighbor, Plot):\n                    width = neighbor.plot_width\n                    height = neighbor.plot_height\n                    break\n            item = Spacer(width=width, height=height)\n        if isinstance(item, LayoutDOM):\n            item.sizing_mode = sizing_mode\n            if isinstance(item, Plot):\n                if plot_width:\n                    item.plot_width = plot_width\n                if plot_height:\n                    item.plot_height = plot_height\n            row_children.append(item)\n        else:\n            raise ValueError("Only LayoutDOM items can be inserted into Grid")\n    tools = tools + row_tools\n    rows.append(Row(children=row_children, sizing_mode=sizing_mode))\n\ngrid = Column(children=rows, sizing_mode=sizing_mode)\n\nif not merge_tools:\n    return grid\n\nif toolbar_location:\n    proxy = ProxyToolbar(tools=tools, **toolbar_options)\n    toolbar = ToolbarBox(toolbar=proxy, toolbar_location=toolbar_location)\n
Run Code Online (Sandbox Code Playgroud)\n

这些工具聚集在一个列表中,将它们分配给特殊的工具栏。我不\xc2\xb4t 看到默认的活动元素是在任何地方收集的。

\n

替代解决方案

\n

因此,您可以做的是手动创建一个工具栏和网格图,您可以在其中为 Toolbar 类设置所需的属性。检查我构建的这个示例:

\n
from bokeh.models import Button, ColumnDataSource, Range1d, Toolbar, ToolbarBox\nfrom bokeh.models.tools import HoverTool, WheelZoomTool, PanTool, CrosshairTool\nfrom bokeh.layouts import layout\nfrom bokeh.plotting import curdoc, figure\n\nx_range = Range1d(start=0, end=10)\ny_range = Range1d(start=0, end=10)\n\n# ------------------- PLOT 1 --------------------------- #\n\nplot_1 = figure(\n    title=\'First figure\',\n    width=400,\n    height=400,\n    x_range=x_range,\n    y_range=y_range,\n    toolbar_location=None,\n    x_axis_label=\'x axis\',\n    y_axis_label=\'y axis\',\n)\n\nx = [1, 2, 3, 4]\ny = [4, 3, 2, 1]\n\nsource = ColumnDataSource(data=dict(x=x, y=y))\n\nplot_1.circle(\n    x=\'x\',\n    y=\'y\',\n    source=source,\n    radius=0.5,\n    fill_alpha=0.6,\n    fill_color=\'green\',\n    line_color=\'black\',\n)\n\n# ------------------- PLOT 2 --------------------------- #\n\nplot_2 = figure(\n    name=\'plot_2\',\n    title=\'Second figure\',\n    width=400,\n    height=400,\n    x_range=x_range,\n    y_range=y_range,\n    toolbar_location=None,\n    x_axis_label=\'x axis\',\n    y_axis_label=\'y axis\',\n)\n\nplot_2.circle(\n    x=\'x\',\n    y=\'y\',\n    source=source,\n    radius=0.5,\n    fill_alpha=0.6,\n    fill_color=\'red\',\n    line_color=\'black\',\n)\n\n# ---------------- ADD TOOLS TO THE PLOT --------------------- #\n\nwheel_zoom = WheelZoomTool()\npan_tool = PanTool()\nhover = HoverTool()\ncrosshair = CrosshairTool()\ntools = (wheel_zoom, pan_tool, hover, crosshair)\n\ntoolbar = Toolbar(\n    tools=[wheel_zoom, pan_tool, hover, crosshair],\n    active_inspect=[crosshair],\n    # active_drag =                         # here you can assign the defaults\n    # active_scroll =                       # wheel_zoom sometimes is not working if it is set here\n    # active_tap \n)\n\ntoolbar_box = ToolbarBox(\n    toolbar=toolbar,\n    toolbar_location=\'left\'\n)\n\nplot_1.add_tools(*tools)\nplot_2.add_tools(*tools)\n\n# ----------------- PLOT LAYOUT -------------------------- #\n\nlayout_1 = layout(\n    children=[\n        [toolbar_box, plot_1, plot_2],\n    ],\n    sizing_mode=\'fixed\',\n)\n\ncurdoc().add_root(layout_1)\n
Run Code Online (Sandbox Code Playgroud)\n

注意:我正在做一些测试,有时效果不佳。这些工具被标记为默认但随机不\xc2\xb4t工作,恐怕它与JavaScript和异步任务有关。所以也许我们应该等待。

\n

第二种替代解决方案

\n

我想我找到了一个始终有效的解决方案。这是一种解决方法。在我的示例中,我使用两个图,但仅显示第一个图的工具栏。无论如何,您需要为两个图设置默认工具栏值。

\n
toolbar_options=dict(logo=\'gray\')\n
Run Code Online (Sandbox Code Playgroud)\n

开发者反馈

\n

事实上,Bryan(散景开发者)在聊天中告诉我

\n
\n

我实际上要回答的是,默认工具激活和网格图从未被一起考虑过,并且尚未得到支持,但如果您找到了适合您的特定用例的东西,那可能是最好的。正如您所说,用户通常不必直接使用工具栏,因为多种原因,他们很挑剔。

\n
\n

  • 不,@chris,正如 Bryan 所说,尚不支持。实际上我会写两个问题(功能请求),一个是在网格图上启用“默认工具激活”,另一个是“直接使用工具栏” (2认同)