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

时间:2019-06-08 11:43:20

标签: python-3.x jupyter-notebook bokeh pandas-bokeh

这是我编写的代码。 我从pandas DF(不在此处粘贴)中获取了数据。 x值来自作为DateTime列的DF索引列。 我要解决的问题符合要求:

TOOLTIPS = [(“ 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)

1 个答案:

答案 0 :(得分:0)

当前(从Bokeh 1.2开始),悬停工具没有任何“ always on”(始终打开)模式,它仅响应于添加到绘图中的命中测试标志符号而悬停。此外,无法将格式应用于$x之类的“特殊变量”。自定义格式化程序只能应用于数据列的悬停工具提示。鉴于此,我最好的建议是改为使用@x(这将询问“ x”数据列,而不是x鼠标位置”。如果这样做,则可以使用{{3 }}部分。

由于您没有提供 complete 示例(无数据可运行),因此我只能提供部分未经测试的建议:

# 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'
相关问题