2016-12-29 66 views
1

我想通過Python中的散景獲得線條圖。我是Bokeh的新手,我試圖將懸停工具提示應用於情節。該圖的x軸具有時間戳值,它們被轉換爲時間戳字符串。我在這裏回顧了一些相同的問題,並試圖對我的案例使用解決方法,但似乎沒有工作。在陰謀它給出???時間應該顯示。Python:散焦懸停日期時間

對我的代碼有任何建議嗎?

時間戳是格式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) 

回答

4

你提示的問題是你創建的源與日期的字符串表示,但p.line()通話是不知道它。所以你必須傳入一個帶有tooltip,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) 

還要注意有關於缺乏的背景虛化提示格式化選項打開的問題。有可能是沒有對datestrings格式化爲一個單獨的列一個簡單的方法:

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

也可以有人告訴我如何格式化x軸蜱垂直顯示?

他們看起來很好,對不起,我無法幫助那個。

希望這會有所幫助!

PS它會更好下一次,如果你發佈的import語句工作腳本和嘲笑了數據幀,使它可以測試。花費了一些時間才把它全部整理出來。但我學習背景虛化,這樣是好的:)

+0

此代碼在python2.7我的0.12.3版本的背景虛化工作非常。但它有一個棄用警告:背景虛化/ UTIL/deprecation.py:33:BokehDeprecationWarning:DatetimeTickFormatter.formats被棄用散景0.12.3和將被刪除,使用單獨的DatetimeTickFormatter領域代替。 警告(消息) –

+0

@PabloReyes是的我也得到了棄用警告。由於它超出了問題的範圍,我沒有考慮它。 :) –