【发布时间】: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