【问题标题】:Changing tool tips to show x's value (datetime) and removing index from the tooltips更改工具提示以显示 x 的值(日期时间)并从工具提示中删除索引
【发布时间】:2019-12-31 09:55:07
【问题描述】:

我正在学习使用散景可视化数据,并且被 HoverTool 及其工具提示所困扰。这是我目前拥有的代码,

from alpha_vantage.timeseries import TimeSeries
import pandas as pd
from pandas.plotting import register_matplotlib_converters
import matplotlib.pyplot as plt
from bokeh.plotting import figure, output_file, show, ColumnDataSource
from bokeh.models import DatetimeTickFormatter, HoverTool

register_matplotlib_converters()
pd.set_option('display.precision',4)
ticker_symbol = 'AAPL'

ts = TimeSeries(key=API_Key, output_format='pandas')
data, meta_data = ts.get_intraday(symbol=ticker_symbol,interval='1min', outputsize='full')

plt_tools = 'hover, pan,wheel_zoom,box_zoom,reset'
p = figure(title='Intraday Times Series', x_axis_label='Time', x_axis_type='datetime', y_axis_label='Price', plot_width=1280, plot_height=960, toolbar_location='below', tools=plt_tools)
h_line = HoverTool()
h_line.mode = 'vline'
h_line.tooltips = [('date','@index'),                          # not sure if this works
                   ('close','$@{4. close}{%0.2f}')]            # not sure if this works
h_line.formatters = {'date': 'datetime', '4. close': 'printf'} # not sure if this works
p.add_tools(h_line)
p.line(data.index.values, data['4. close'], legend_label=ticker_symbol, line_width=2)

output_file('lines.html')
show(p)

数据看起来像这样

                      1. open   2. high    3. low  4. close  5. volume
date                                                                  
2019-12-23 09:35:00  281.0400  281.3582  281.0400  281.3582   171044.0
2019-12-23 09:34:00  281.0400  281.0400  281.0400  281.0400   129570.0
2019-12-23 09:33:00  281.3100  281.3900  281.2100  281.3300    97498.0
2019-12-23 09:32:00  281.4400  281.4800  281.1600  281.2800   194802.0
2019-12-23 09:31:00  281.4246  281.4246  281.4246  281.4246   957947.0

我设法让 HoverTool 和 vline 工作,但我从情节中得到 2 个工具提示。 相互堆叠(stacked tooltips) 以及没有 h_line.tooltips 和 h_line.formatters 的原始工具提示 (original tooltip)

如何将工具提示更改为像底部的块一样显示,而不是在同一行显示科学数字和价格:

Date: DD-MM-YY HH:MM 
Close: xxx.xx 

日期示例 - 01-01-20 13:15
关闭示例 - 291.86

【问题讨论】:

    标签: bokeh python-3.7


    【解决方案1】:

    这里的问题是您将数据直接传递给p.line,而不是创建ColumnDataSource 并从那里引用数据。当您将数据直接传递给字形函数时,Bokeh 必须为您创建一个 ColumnDataSource,并且由于您没有告诉它为列使用什么名称,它只能使用通用名称,如 'x''y' (例如在这种情况下)。这些通用名称与您为悬停工具配置的列名称不匹配。您可以:

    • 更改悬停工具配置以使用通用列名称'x''y',或

    • 创建一个ColumnDataSource 并将其作为source 传递给p.line

    请注意,如果您希望其他列也可用于悬停工具,则必须使用第二个选项,以及您希望该工具可用的所有列。

    有关所有这些信息,请参阅用户指南章节Providing Data

    【讨论】:

    • 感谢您为我指明正确的方向。在“提供数据”之后,我设法使用data = {'x_values': [1, 2, 3, 4, 5],'y_values': [6, 7, 2, 3, 6]},但将ColumnDataSource(data)(实际数据框作为数据引用,该数据使用问题中的日期索引打印表格)返回一个错误,上面写着如果我在 p.line 中传递 'source=source',我将不得不引用它或放入数据序列中。有没有办法打印 ColumnDataSource(data) 数据框,以便我可以看到我需要引用 p.line 的内容?
    • 如果您使用 source = ColumnDataSource(df) 从 DataFrame 制作 CDS,CDS 中的列通常与 DataFrame 开头的任何列相同(可能加上 index 列)。当然,你也可以直接检查类似dict的source.data,看看它有什么键。如果您将source 传递给p.line,那么您需要将列名 用于x 和y 坐标等内容。例如p.line('some_xcol', 'other_ycol', source=source)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-06
    • 1970-01-01
    • 2019-12-20
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多