python-3.x - Bokeh 上的 HoverTool:时间格式问题(日期显示不正确)x 轴来自 pandas 中的 datetime DF 列
问题描述
这里是我写的代码。我从 pandas DF 获取数据(此处未粘贴)。x 值来自作为 DateTime 列的 DF 索引列。我要解决的问题是:
工具提示 = [("index", "$index"),("(时间,温度)", "($x, $y)"),]
当我必须将 $x 格式更改为正确的格式以便在散景图上的悬停窗口中查看时间格式时。
看python代码
import datetime as dt
from bokeh.plotting import figure, output_file, show
from bokeh.layouts import gridplot
from bokeh.models import ColumnDataSource, CDSView, BooleanFilter
from bokeh.models import DatetimeTickFormatter
x=df_bases.index
y0=df_bases["base_1"]
y1=df_bases["base_5"]
y2=df_bases["base_12"]
# output to static HTML file
output_file("temperatures from thermocouples.html")
# add some renderers
output_file("Thermocouples temperature.html", title="Thermocouples temperature")
TOOLTIPS = [("index", "$index"),("(Time,Temperature)", "($x, $y)"),]
# create a new plot with a datetime axis type
p = figure( tooltips=TOOLTIPS , plot_width=1250, plot_height=580, x_axis_type="datetime", x_axis_label='Time',
y_axis_label='Temperature [°C]', title="Thermocouples temperature")
p.line(x, y0, legend="thermocouple 1", line_width=1 , color='navy', alpha=1)
p.line(x, y1, legend="thermocouple 5", color="green")
p.line(x, y2, legend="thermocouple 12", line_width=1 , color='orange', alpha=1)#, line_dash="4 4")
p.border_fill_color = "whitesmoke"
p.xaxis.formatter=DatetimeTickFormatter(
microseconds = ['%Y-%m-%d %H:%M:%S.%f'],
milliseconds = ['%Y-%m-%d %H:%M:%S.%3N'],
seconds = ["%Y-%m-%d %H:%M:%S"],
minsec = ["%Y-%m-%d %H:%M:%S"],
minutes = ["%Y-%m-%d %H:%M:%S"],
hourmin = ["%Y-%m-%d %H:%M:%S"],
hours=["%Y-%m-%d %H:%M:%S"],
days=["%Y-%m-%d %H:%M:%S"],
months=["%Y-%m-%d %H:%M:%S"],
years=["%Y-%m-%d %H:%M:%S"],
)
p.title.align = 'center'
# create a column data source for the plots to share
source = ColumnDataSource(data=dict(x=x, y0=y0, y1=y1, y2=y2))
# create a view of the source for one plot to use
view = CDSView(source=source)
# show the results
show(p)
解决方案
目前(从 Bokeh 1.2 开始)悬停工具没有任何“始终开启”模式它只悬停以响应添加到绘图中的命中测试字形。此外,无法将格式应用于“特殊变量”之类$x
的(从 Bokeh 2.0 开始就有可能)。自定义格式化程序只能应用于数据列的悬停工具提示。鉴于此,我最好的建议是改用使用@x
(它询问“x”数据列,而不是 x 鼠标位置”。如果这样做,您可以使用文档的“格式化工具提示字段”部分中的所有技术。
由于您没有提供完整的示例(没有要运行的数据),我只能提供部分未经测试的建议:
# use @x{%F} to specify the %F datetime format (or choose another) for the x column
TOOLTIPS = [("index", "$index"),("(Time,Temperature)", "(@x{%F}, $y)")]
# tell bokeh to use the "datetime" formatter for the x column
p.hover.formatters = {'x': 'datetime'}
# just a suggestion, often useful for timeseries plots
p.hover.mode = 'vline'
推荐阅读
- rendering - D3D11 CreateDeferredContext 错误,但从未创建延迟渲染上下文
- node.js - NodeJS:强制“正确”导入名称的 ESLint 规则
- c# - 使用运算符 c# 进行规则验证
- c - CS50 复数:源代码中的 return 2 是什么意思?
- vb.net - 在多个分隔符之间拆分文本
- css - Grid vs Flex 用于实现类似圣杯的布局
- performance - 有没有办法加速/批量谷歌日历读/写?
- python - Kivy 滑块触发器切换按钮
- python - 从 pandas.read_sql_query().to_dict() 获取下一个键并用于字典理解
- python - 获取元素而不是 xpath 的另一种方法