【问题标题】:trying to plot mouse hovering interactive graph with bokeh in python试图在python中用散景绘制鼠标悬停交互式图形
【发布时间】:2021-05-15 16:31:48
【问题描述】:

我是散景新手并试图绘制图表。我有三个列表说,

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)。

【问题讨论】:

    标签: python matplotlib graph hover bokeh


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-19
      • 1970-01-01
      • 2016-12-31
      相关资源
      最近更新 更多