2017-10-20 61 views
0

這是怎樣的一個複雜的例子,但我拼命地希望能得到幫助...push_notebook不更新的背景虛化的圖表

我使用jupyter-notebook 5.2.0bokeh版本是0.12.9ipywidgets7.0.1

這裏是我的DataFrame df

import numpy as np 
import pandas as pd 
import datetime 
import string 

start = int(datetime.datetime(2017,1,1).strftime("%s")) 
end = int(datetime.datetime(2017,12,31).strftime("%s")) 

# set parameters of DataFrame df for simualtion 
size, numcats = 100,10 

rints = np.random.randint(start, end + 1, size = size) 
df = pd.DataFrame(rints, columns = ['zeit']) 
df["bytes"] = np.random.randint(5,20,size=size) 
df["attr1"] = np.random.randint(5,100,size=size) 
df["ind"] = ["{}{}".format(i,j) for i in string.ascii_uppercase for j in string.ascii_uppercase][:len(df)] 

choices = list(string.ascii_uppercase)[:numcats] 
df['who']= np.random.choice(choices, len(df)) 
df["zeit"] = pd.to_datetime(df["zeit"], unit='s') 
df.zeit = df.zeit.dt.date 

df.sort_values('zeit', inplace = True) 
df = df.reset_index(drop=True) 
df.head(3) 

enter image description here

現在,讓我們創建一個柱狀圖中,還使用懸停工具:

from bokeh.io import show, output_notebook, push_notebook 
from bokeh.models import ColumnDataSource, HoverTool 
from bokeh.plotting import figure 
import ipywidgets as widgets 
output_notebook() 

# setup figure 
hover = HoverTool(tooltips=[ 
    ("index", "$index"), 
    ("ind", "@ind"), 
    ("who", "@who"), 
    ("bytes", "@bytes"), 
    ("attr1", "@attr1"), 
]) 

fig = figure(x_range=list(df.ind), plot_height=250, title="Test Bars", 
      toolbar_location=None, tools=[hover]) 
x = fig.vbar(x="ind", top="bytes", width=0.9, source=ColumnDataSource(df)) 
h=show(fig, notebook_handle=True) 

enter image description here

我使用一個ipywidgets.widgets.SelectionRangeSlider選擇ECT日期範圍:

import ipywidgets as widgets 

# create slider 
dates = list(pd.date_range(df.zeit.min(), df.zeit.max(), freq='D')) 
options = [(i.strftime('%d.%m.%Y'), i) for i in dates] 
index = (0, len(dates)-1) 
myslider = widgets.SelectionRangeSlider(
    options = options, 
    index = index, 
    description = 'Test', 
    orientation = 'horizontal', 
    layout={'width': '500px'} 
) 

def update_source(df, start, end): 
    x = df[(df.zeit >= start) & (df.zeit < end)] 
    #data = pd.DataFrame(x.groupby('who')['bytes'].sum()) 
    #data.sort_values(by="bytes", inplace=True) 
    #data.reset_index(inplace=True) 
    #return data 
    return x 

def gui(model, bars): 
    def myupdate(control1): 
     start = control1[0].date() 
     end = control1[1].date() 
     #display(update_source(model, start, end).head(4)) 
     data = update_source(model, start, end) 
    return myupdate 

widgets.interactive(gui(df, x), control1 = myslider) 

enter image description here

的問題是,我不能從部件獲得一個更新圖:

x.data_source = ColumnDataSource(update_source(df, myslider.value[0].date(), myslider.value[1].date())) 
push_notebook(handle=h) 

至少,它用的東西情節,因爲hover不能工作了...

我錯過了什麼?或者這是一個錯誤?

感謝所有幫助

馬庫斯

+0

你應該試着讓你現有的CDS改變它的'.data'屬性,而不是一起取代CDS。這是一個更加行使的使用模式。 – bigreddot

+0

好的,我嘗試'newdf = update_source(df,myslider.value [0] .date(),myslider.value [1] .date())'和'x.data_source.data [「ind」] = newdf。 ind'和'給出'BokehUserWarning':'ColumnDataSource的列必須具有相同的長度。「並且圖不會更新。我認爲最基本的問題是,vbar的指數會減少一些......有沒有一些工作的例子? – Markus

+0

如果您使用'x.data_source = ColumnDataSource(newdf)',則不會引發BokehUserWarning。如果您進一步添加'fig.x_range.factors = newdf.ind'和'push_notebook(handle = h)',則繪圖正確更新。但懸停不起作用。任何想法如何讓懸停工作? – Markus

回答