我正在尝试在Python的Bokeh中制作一条线形图。我对Bokeh不熟悉,正在尝试在图表上应用悬停提示。该图的x轴具有时间戳值,这些值被转换为时代字符串。我已经查看了一些类似问题,并尝试使用解决方法来解决我的问题,但似乎没有起作用。在图表上,应显示时间的位置会出现
???
。
对于我的代码,有什么建议吗?
时间戳格式为2016-12-29 02:49:12
另外,有人可以告诉我如何格式化x轴
刻度以垂直显示吗?
p = figure(width=1100,height=300,tools='resize,pan,wheel_zoom,box_zoom,reset,previewsave,hover',logo=None)
p.title.text = "Time Series for Price in Euros"
p.grid.grid_line_alpha = 0
p.xaxis.axis_label = "Day"
p.yaxis.axis_label = "Euros"
p.ygrid.band_fill_color = "olive"
p.ygrid.band_fill_alpha = 0.1
p.circle(df['DateTime'],df['EuP'], size=4, legend='close',
color='darkgrey', alpha=0.2)
p.xaxis.formatter = DatetimeTickFormatter(formats=dict(
hours=["%d %B %Y"],
days=["%d %B %Y"],
months=["%d %B %Y"],
years=["%d %B %Y"],
))
source = ColumnDataSource(data=dict(time=[x.strftime("%Y-%m-%d %H:%M:%S")for x in df['DateTime']]))
hover = p.select(dict(type=HoverTool))
hover.tooltips = {"time":'@time', "y":"$y"}
hover.mode = 'mouse'
p.line(df['DateTime'],df['EuP'],legend='Price',color='navy',alpha=0.7)
@Datetime
,它才对我起作用。 - racket99hover_tool.formatters = { "$x": "datetime"}
代码来处理特殊的$x
变量,但是失败了,最终我只能回到类似于HoverTool(tooltips=[('date', '@DateTime{%F %T}')], formatters={'@DateTime': 'datetime'})
的方案。哦,HoverTool(tooltips=[('date', '$x{%F %T}')], formatters={'$x': 'datetime'})
也可以。 - Dave X