具有不同工具提示的多个散景图

时间:2020-03-30 19:48:54

标签: bokeh

这是我要实现的目标的最小示例

from bokeh.plotting import ColumnDataSource, figure, output_file, save                                                                                                                                                                                                                                                                                  
output_file("test_plot.html")                                                                                                                                 
source = ColumnDataSource(data=dict(
                                x=[1,3],
                                y=[3,7],
                                label=['a','b']
                                ))

TOOLTIPS = [ ("label", "@label")]

p = figure(plot_width=400, plot_height=400, tooltips=TOOLTIPS, title="plot")

p.circle('x', 'y', size=20,fill_alpha=0.5, source=source)


source = ColumnDataSource(data=dict(
                                x=[0],
                                y=[0],
                                info1 = ['info 1'],
                                info2 = ['info 2']
                                ))


p.circle('x','y', size=10,fill_alpha=0.5, source=source)

save(p)

我需要在(0,0)处向散点图添加另一个TOOLTIPS,它可以显示info1和info2。

1 个答案:

答案 0 :(得分:1)

您必须手动创建工具。请注意,它还将在工具栏上显示多个工具。

from bokeh.models import HoverTool
from bokeh.plotting import ColumnDataSource, figure, save

source = ColumnDataSource(data=dict(x=[1, 3], y=[3, 7],
                                    label=['a', 'b']))

p = figure(plot_width=400, plot_height=400, title="plot")

circle1 = p.circle('x', 'y', size=20, fill_alpha=0.5, source=source)

source = ColumnDataSource(data=dict(x=[0], y=[0],
                                    info1=['info 1'], info2=['info 2']))

circle2 = p.circle('x', 'y', size=10, fill_alpha=0.5, source=source)

p.add_tools(HoverTool(renderers=[circle1],
                      tooltips=[('label', '@label')]),
            HoverTool(renderers=[circle2],
                      tooltips=[('info1', '@info1'),
                                ('info2', '@info2')]))

save(p)