散景上的悬停工具:时间格式问题(日期显示不正确)来自熊猫中日期时间 DF 列的 x 轴



这是我写的代码。我从熊猫DF获取了数据(此处未粘贴(。x 值来自作为日期时间列的 DF 索引列。我想解决的问题符合:

工具提示 = [("索引", "$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)

目前(从散景 1.2 开始(,悬停工具没有任何"始终开启"模式 它仅在响应添加到图中的命中测试字形时悬停。此外,无法将格式应用于像$x这样的"特殊变量"(从散景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'

最新更新