试图在python中用散景绘制鼠标悬停交互式图形

时间:2021-05-15 16:31:48

标签: python matplotlib graph hover bokeh

我是散景的新手,正在尝试绘制图表。我有三个列表说,

from bokeh.plotting import figure, show
x=[1,2,3,4,5,6,7,8,9,10,11]
y=[1,2,1,1,1,1,3,4,5,5,5]
c=[50,40,30,20,10,60,50,40,30,20,10]

p = figure(x_axis_type="datetime", title="Range", plot_height=350, plot_width=800)
p.xgrid.grid_line_color=None
p.ygrid.grid_line_alpha=0.5
p.xaxis.axis_label = 'Time'
p.yaxis.axis_label = 'Value'

p.line(x,y)
show(p)

我想要一种像阶跃函数图这样的时间序列,其中 x 轴是连续的时间序列(列表 x),y 轴是事件(列表 y),即 y 轴应该只标记到 5(如 1、2、3、4、5)并且鼠标指针悬停在绘制的点上时应显示存储在 c 中的相应值。

例如,当时间为 x=1,然后 y=1,c=50 时。

这样我就可以通过查看人所在位置的 x 轴(y 轴上的 5 个位置中的 1、2、3、4、5 位置)并通过放置我的鼠标来知道值是多少当时(名单c)。

1 个答案:

答案 0 :(得分:1)

如果您只想在特定点显示工具提示,我会添加圆圈并将它们设置为唯一的悬停渲染器,如下所示:

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

x=[1,2,3,4,5,6,7,8,9,10,11]
y=[1,2,1,1,1,1,3,4,5,5,5]
c=[50,40,30,20,10,60,50,40,30,20,10]
source = ColumnDataSource({'x': x, 'y': y, 'c': c})

p = figure(x_axis_type="datetime", title="Range", plot_height=350, plot_width=800, tooltips = [('time', '@x'), ('place', '@y'), ('value','@c')])
p.xgrid.grid_line_color=None
p.ygrid.grid_line_alpha=0.5
p.xaxis.axis_label = 'Time'
p.yaxis.axis_label = 'Value'

lines = p.line('x','y', source=source)
circles = p.circle('x','y', source=source)

p.select_one(HoverTool).renderers = [circles]

show(p)
相关问题