【发布时间】:2019-03-19 15:04:12
【问题描述】:
我正在使用 Bokeh 尝试创建一个图形,其数据点在用户“悬停”时将在悬停工具中显示另一个图形,显示有关该数据点的其他信息(即,在主图中数据点是设定间隔内时间序列的平均值,我希望悬停工具显示该间隔内的所有数据)。
user guide(完整代码复制在下面)提供了一种解决方案:使用自定义 HTML 工具提示来引用文件中的数字。但是,这需要我创建所有存档的数字(可能多达 10,000 个)以供参考。这是一个太大的时间开销,所以我希望有一个更好的解决方案。 即:悬停工具是否可以动态运行 python 代码,以便它们可以交互地显示数据图?
(示例图片,取自用户指南,以下代码)
以下代码是 2019 年 3 月 19 日从bokeh user guide 复制而来的。
from bokeh.plotting import figure, output_file, show, ColumnDataSource
output_file("toolbar.html")
source = ColumnDataSource(data=dict(
x=[1, 2, 3, 4, 5],
y=[2, 5, 8, 2, 7],
desc=['A', 'b', 'C', 'd', 'E'],
imgs=[
'http://docs.bokeh.org/static/snake.jpg',
'http://docs.bokeh.org/static/snake2.png',
'http://docs.bokeh.org/static/snake3D.png',
'http://docs.bokeh.org/static/snake4_TheRevenge.png',
'http://docs.bokeh.org/static/snakebite.jpg'
],
fonts=[
'<i>italics</i>',
'<pre>pre</pre>',
'<b>bold</b>',
'<small>small</small>',
'<del>del</del>'
]
))
TOOLTIPS = """
<div>
<div>
<img
src="@imgs" height="42" alt="@imgs" width="42"
style="float: left; margin: 0px 15px 15px 0px;"
border="2"
></img>
</div>
<div>
<span style="font-size: 17px; font-weight: bold;">@desc</span>
<span style="font-size: 15px; color: #966;">[$index]</span>
</div>
<div>
<span>@fonts{safe}</span>
</div>
<div>
<span style="font-size: 15px;">Location</span>
<span style="font-size: 10px; color: #696;">($x, $y)</span>
</div>
</div>
"""
p = figure(plot_width=400, plot_height=400, tooltips=TOOLTIPS,
title="Mouse over the dots")
p.circle('x', 'y', size=20, source=source)
show(p)
【问题讨论】: