更改工具提示以显示 x 的值(日期时间)并从工具提示中删除索引
Changing tool tips to show x's value (datetime) and removing index from the tooltips
我正在学习使用散景可视化数据,并且一直使用 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
这里的问题是您将数据直接传递给 p.line
,而不是创建 ColumnDataSource
并从那里引用数据。当您将数据直接传递给字形函数时,Bokeh 必须为您创建一个 ColumnDataSource
,并且由于您没有告诉它要为列使用什么名称,它只能使用通用名称,例如 'x'
和 'y'
(例如在这种情况下)。这些通用名称与您为悬停工具配置的列名称不匹配。您可以:
更改悬停工具配置以使用通用列名称 'x'
和 'y'
,或
创建一个 ColumnDataSource
并将其作为 p.line
的 source
传递给 p.line
。
请注意,如果您希望其他列也可用于悬停工具,则必须使用第二个选项,以及您希望该工具可用的所有列。
有关所有这些信息,请参阅用户指南第 Providing Data 章。
我正在学习使用散景可视化数据,并且一直使用 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
这里的问题是您将数据直接传递给 p.line
,而不是创建 ColumnDataSource
并从那里引用数据。当您将数据直接传递给字形函数时,Bokeh 必须为您创建一个 ColumnDataSource
,并且由于您没有告诉它要为列使用什么名称,它只能使用通用名称,例如 'x'
和 'y'
(例如在这种情况下)。这些通用名称与您为悬停工具配置的列名称不匹配。您可以:
更改悬停工具配置以使用通用列名称
'x'
和'y'
,或创建一个
ColumnDataSource
并将其作为p.line
的source
传递给p.line
。
请注意,如果您希望其他列也可用于悬停工具,则必须使用第二个选项,以及您希望该工具可用的所有列。
有关所有这些信息,请参阅用户指南第 Providing Data 章。