我试图通过Python中的Bokeh获得一个线条图。我是Bokeh的新手,我正试图在情节上应用悬停工具提示。绘图的x轴具有Timestamp值,将其转换为epoch字符串。我已经在这里审查了一些相同的问题,并试图将解决方法用于我的案例,但它似乎并没有起作用。在情节上,它给出???
时间应该出现的地方。
我的代码有什么建议吗?
时间戳格式为2016-12-29 02:49:12
也有人可以告诉我如何格式化x-axis
刻度以垂直显示?
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)
答案 0 :(得分:12)
由于这个答案最初发布,因此新的工作进入了Bokeh,使事情更简单。可以通过指定格式化程序直接通过悬停工具将日期时间字段格式化为日期时间,例如:
HoverTool(tooltips=[('date', '@DateTime{%F}')],
formatters={'DateTime': 'datetime'})
不再需要预先格式化数据源中的日期字段,如下所示。有关详细信息,请参阅Formatting Tooltip Fields
OLD ANSWER :
您的工具提示的问题是您使用日期的字符串表示创建了一个源,但p.line()
调用并不知道它。所以你必须传入一个包含工具提示,x和y值的columndatasource。
以下是您的代码的工作变体:
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)
另请注意,散景工具提示中缺少格式化选项存在未解决的问题。可能有一种更简单的方法可以不将日期字符串格式化为单独的列:
https://github.com/bokeh/bokeh/issues/1239
也有人可以告诉我如何格式化x轴刻度以垂直显示?
他们看起来很好,对不起,我无法帮助那个。
希望这有帮助!
PS如果你发布一个包含import语句的工作脚本,并且使用一个模拟的数据帧来测试它,那么下次会更好。花了一些时间来整理它。但我正在学习散景,所以很好:)
答案 1 :(得分:11)
很抱歉没有发表评论,我对此没有足够的声誉。
@Alex接受的答案对我不起作用(散景2.0.1),因为格式化程序中缺少简单的@符号。工作代码如下:
<link href="{{ asset('css/app.css') }}" rel="stylesheet" />
<script src="{{ asset('js/app.js') }}" type="text/javascript"></script>
答案 2 :(得分:0)
我已经在bokeh中为ScatterPlot创建了包装。
from visualization import ScatterChart
sc = ScatterChart(
df,
{'x_axis' :'ts',
'y_axis': 'Discus',
'series_column': 'Competition',
'xlabel':'Discus',
'ylabel':'Javeline',
'title':'Discus Vs Javeline'
})
d = sc.render()
show(d)
这就是我初始化的方式
{{1}}