Python:Bokeh悬停日期时间 [英] Python: Bokeh hover date time

查看:91
本文介绍了Python:Bokeh悬停日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过Bokeh在Python中获得线图.我是Bokeh的新手,正在尝试将悬停工具提示应用于该图.绘图的x轴具有时间戳记值,这些值会转换为纪元字符串.我在这里检查了一些相同的问题,并尝试对我的案例使用解决方法,但似乎不起作用.在情节上,它给出了???应该显示时间的位置.

I am trying to get a line plot via Bokeh in Python. I am new to Bokeh and I am trying to apply hover tool tips over the plot. The x-axis of the plot has Timestamp values which are converted into epoch string. I've reviewed some same problems here and tried to use the workaround for my case but it doesn't seem to work. On the plot it gives ??? where the time should show up.

对我的代码有什么建议吗?

Any suggestions for my code?

时间戳的格式为2016-12-29 02:49:12

还可以告诉我如何格式化x-axis刻度线以垂直显示吗?

Also can someone tell how do I format x-axis ticks to show up vertically ?

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)

推荐答案

自从最初发布此答案以来,Bokeh进行了新的工作以使事情变得更简单.可以通过悬停工具直接将datetime字段格式化为datetime,方法是指定格式化程序,例如:

Since this answer was originally posted, new work has gone into Bokeh to make things simpler. A datetime field can be formatted as a datetime directly by the hover tool, by specifying a formatter, e.g.:

HoverTool(tooltips=[('date', '@DateTime{%F}')],
          formatters={'DateTime': 'datetime'})

不再需要如下所示预先格式化数据源中的日期字段.有关更多信息,请参见格式化工具提示字段

It is no longer necessary to pre-format date fields in the data source as below. For more information see Formatting Tooltip Fields

旧答案:

您的工具提示存在的问题是您使用日期的字符串表示形式创建了一个源,但是p.line()调用并未意识到这一点.因此,您必须传入具有工具提示,x和y值的columndatasource.

The problem with your tooltip is you created a source with the string representation of the dates, but the p.line() call is unaware of it. So you have to pass in a columndatasource that has the tooltip, the x and y values.

这是您的代码的有效变体形式:

Here is a working variant of your code:

from bokeh.plotting import figure, show
from bokeh.models.formatters import DatetimeTickFormatter
from bokeh.models import ColumnDataSource
from bokeh.models.tools import HoverTool
import pandas as pd
import numpy as np

data = {
    'DateTime' : pd.Series(
        ['2016-12-29 02:49:12',
        '2016-12-30 02:49:12',
        '2016-12-31 02:49:12'],
        dtype='datetime64[ns]'),
    'EuP' : [20,40,15]
}
df = pd.DataFrame(data)
df['tooltip'] = [x.strftime("%Y-%m-%d %H:%M:%S") for x in df['DateTime']]
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"],
))
hover = p.select(dict(type=HoverTool))
tips = [('when','@tooltip'), ('y','$y')]
hover.tooltips = tips
hover.mode = 'mouse'
p.line(x='DateTime', y='EuP', source=ColumnDataSource(df),
       legend='Price',color='navy',alpha=0.7)
show(p)

还请注意,关于bokeh工具提示中缺少格式设置选项存在一个未解决的问题.可能有一种更简单的方法,不必将日期字符串格式化为单独的列:

Also note there is an open issue about the lack of formatting options in the bokeh tooltip. There might be an easier way to not have to format the datestrings as a separate column:

https://github.com/bokeh/bokeh/issues/1239

还可以告诉我如何格式化x轴刻度以垂直显示吗?

Also can someone tell how do I format x-axis ticks to show up vertically ?

他们对我来说还不错,对不起,我不能帮上忙.

They look fine to me, sorry I cannot help on that one.

希望这会有所帮助!

PS,如果您发布带有导入语句的工作脚本以及模拟的数据框以进行测试,那么下一次会更好.花了一些时间来解决所有问题.但是我正在学习Bokeh,所以很好:)

PS it would be better next time if you posted a working script with import statements, and a mocked up dataframe to make it possible to test. It took some time to sort it all out. But I am learning Bokeh so that is fine :)

这篇关于Python:Bokeh悬停日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆