尝试在 python 中绘制带有散景的鼠标悬停交互式图形

trying to plot mouse hovering interactive graph with bokeh in python

我是散景的新手,正在尝试绘制 graph.I 有三个列表,

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 -axis 应该只标记到 5(如 1,2,3,4,5),当鼠标指针悬停在绘图点上时应该显示存储在 c 中的相应值。

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

这样我就可以通过查看 x 轴知道那个人在什么时间(在 y 轴上的 5 个位置 1、2、3、4、5 中)并通过将我的鼠标放在什么地方当时的值(列表c)。

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

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)