Bokeh TapTool 不适用于区域字形

Bokeh TapTool doesnt work for area glyphs

点击工具似乎不适用于面积图。有其他选择吗? 下面的示例代码 散景版本:'2.0.2'

from bokeh.plotting import figure, output_file, show
from bokeh.models import TapTool, CustomJS, VArea


p = figure(plot_width=400, plot_height=400)
p.grid.minor_grid_line_color = '#eeeeee'

area = p.varea(x=[1, 2, 3, 4, 5],
        y1=[2, 6, 4, 3, 5],
        y2=[1, 4, 2, 2, 3],name="temp")

p.add_tools(TapTool(callback=CustomJS(code="console.log('taptool')")))
show(p)

# Nothing comes up
# It works with other glyphs for instance a circle

area = p.circle(x=20,y=20,size=15,)
p.add_tools(TapTool(callback=CustomJS(code="console.log('taptool')"))) 
show(p) # works as expected

是的,还有一个选择。您可以使用多边形代替 Varea。对于多边形,您必须使用另一种语法,但您可以创建相同的图形并且 TapTool 可以正常工作。

from bokeh.plotting import figure show, output_notebook
from bokeh.models import TapTool, CustomJS
output_notebook()

p = figure(plot_width=400, plot_height=400)
#p.grid.minor_grid_line_color = '#eeeeee'

p.multi_polygons(xs=[[ [ [1, 2, 3, 4, 5, 5, 4, 3, 2, 1] ]]],
                 ys=[[ [ [1, 4, 2, 2, 3, 5, 3, 4, 6, 2] ]]])

p.circle(x=20,y=20,size=15,)

p.add_tools(TapTool(callback=CustomJS(code="console.log('taptool')"))) 
show(p)

我也试过补丁和区域的解决方案,但我不知道为什么它不起作用。您是否考虑过在 github 上开一个问题?

根据 mosc9575 的回答,我切换到多边形字形来绘制堆叠区域。以下是我复制示例的代码:https://docs.bokeh.org/en/latest/docs/gallery/stacked_area.html

from bokeh.plotting import figure, output_file, show
from bokeh.models import TapTool, CustomJS
import pandas as pd
import numpy as np
from bokeh.palettes import brewer
from functools import reduce

output_file("patch.html")

N = 10
df = pd.DataFrame(np.random.randint(10, 100, size=(15, N))).add_prefix("y")


def rec_add(a, b):
    a = df[a] if isinstance(a, str) else a
    df[b] = a + df[b]
    return df[b]


reduce(rec_add, df.columns)

names = ["y%d" % i for i in range(N)]
p = figure(x_range=(0, len(df) - 1), y_range=(0, 800))
p.multi_polygons(
    xs=[[[np.concatenate([df.index.values, df.index.values[::-1]])]]] * 10,
    ys=[
        [
            [
                np.concatenate(
                    [
                        df[f"y{i}"].values,
                        df[f"y{i-1}"].values[::-1] if i > 0 else np.zeros((len(df),)),
                    ]
                )
            ]
        ]
        for i in range(len(df.columns))
    ],
    color=brewer["Spectral"][N],
)
p.add_tools(
    TapTool(callback=CustomJS(code="console.log(cb_data.source.selected.indices)")) 
)

show(p)