散景TapTool不适用于区域字形

时间:2020-11-11 09:43:26

标签: python-3.x bokeh

点击工具似乎不适用于面积图。还有其他选择吗? 下面的示例代码 散景版本:“ 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

2 个答案:

答案 0 :(得分:1)

是的,还有另一种选择。您可以使用多边形而不是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上发布问题?

答案 1 :(得分:0)

在回答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)