在Bokeh中,如何向时间序列图(悬停工具)添加工具提示? [英] In Bokeh, how do I add tooltips to a Timeseries chart (hover tool)?

查看:362
本文介绍了在Bokeh中,如何向时间序列图(悬停工具)添加工具提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在时间序列图表中添加工具提示?

Is it possible to add Tooltips to a Timeseries chart?

在下面的简化代码示例中,当鼠标悬停在相关行上时,我想看到一个列名("a","b"或"c").

In the simplified code example below, I want to see a single column name ('a','b' or 'c') when the mouse hovers over the relevant line.

相反,是"???"会显示,并且所有三行都将显示工具提示(而不仅仅是将鼠标悬停在上面)

Instead, a "???" is displayed and ALL three lines get a tool tip (rather than just the one im hovering over)

根据文档( http://docs.bokeh.org/en/latest/docs/user_guide/tools.html#hovertool ),以"@"开头的字段名称将解释为数据源上的列.

Per the documentation ( http://docs.bokeh.org/en/latest/docs/user_guide/tools.html#hovertool), field names starting with "@" are interpreted as columns on the data source.

  1. 如何在工具提示中显示熊猫DataFrame中的列"?

  1. How can I display the 'columns' from a pandas DataFrame in the tooltip?

或者,如果高级TimeSeries接口不支持此功能,那么使用低级接口执行相同操作的任何线索是什么? (line?multi_line?)或将DataFrame转换为其他格式(ColumnDataSource?)

Or, if the high level TimeSeries interface doesn't support this, any clues for using the lower level interfaces to do the same thing? (line? multi_line?) or convert the DataFrame into a different format (ColumnDataSource?)

要获得赠金,应如何设置"$ x"的格式以将日期显示为日期?

For bonus credit, how should the "$x" be formatted to display the date as a date?

预先感谢

    import pandas as pd
    import numpy as np
    from bokeh.charts import TimeSeries
    from bokeh.models import HoverTool
    from bokeh.plotting import show

    toy_df = pd.DataFrame(data=np.random.rand(5,3), columns = ('a', 'b' ,'c'), index = pd.DatetimeIndex(start='01-01-2015',periods=5, freq='d'))   

    p = TimeSeries(toy_df, tools='hover')  

    hover = p.select(dict(type=HoverTool))
    hover.tooltips = [
        ("Series", "@columns"),
        ("Date", "$x"),
        ("Value", "$y"),
        ]

    show(p)

推荐答案

下面是我的想法.

它不漂亮,但是可以用.

Its not pretty but it works.

我对Bokeh仍然是陌生的(在此情况下为& Python),因此,如果有人想提出一种更好的方法来做到这一点,请放心.

Im still new to Bokeh (& Python for that matter) so if anyone wants to suggest a better way to do this, please feel free.

import pandas as pd
import numpy as np
from bokeh.charts import TimeSeries
from bokeh.models import HoverTool
from bokeh.plotting import show

toy_df = pd.DataFrame(data=np.random.rand(5,3), columns = ('a', 'b' ,'c'), index = pd.DatetimeIndex(start='01-01-2015',periods=5, freq='d'))       

 _tools_to_show = 'box_zoom,pan,save,hover,resize,reset,tap,wheel_zoom'        

p = figure(width=1200, height=900, x_axis_type="datetime", tools=_tools_to_show)


# FIRST plot ALL lines (This is a hack to get it working, why can't i pass in a dataframe to multi_line?)   
# It's not pretty but it works. 
# what I want to do!: p.multi_line(df)
ts_list_of_list = []
for i in range(0,len(toy_df.columns)):
    ts_list_of_list.append(toy_df.index.T)

vals_list_of_list = toy_df.values.T.tolist()

# Define colors because otherwise multi_line will use blue for all lines...
cols_to_use =  ['Black', 'Red', 'Lime']
p.multi_line(ts_list_of_list, vals_list_of_list, line_color=cols_to_use)


# THEN put  scatter one at a time on top of each one to get tool tips (HACK! lines with tooltips not yet supported by Bokeh?) 
for (name, series) in toy_df.iteritems():
    # need to repmat the name to be same dimension as index
    name_for_display = np.tile(name, [len(toy_df.index),1])

    source = ColumnDataSource({'x': toy_df.index, 'y': series.values, 'series_name': name_for_display, 'Date': toy_df.index.format()})
    # trouble formating x as datestring, so pre-formating and using an extra column. It's not pretty but it works.

    p.scatter('x', 'y', source = source, fill_alpha=0, line_alpha=0.3, line_color="grey")     

    hover = p.select(dict(type=HoverTool))
    hover.tooltips = [("Series", "@series_name"), ("Date", "@Date"),  ("Value", "@y{0.00%}"),]
    hover.mode = 'mouse'

show(p)

这篇关于在Bokeh中,如何向时间序列图(悬停工具)添加工具提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆