这是怎样的一个复杂的例子,但我拼命地希望能得到帮助...push_notebook不更新的背景虚化的图表
我使用jupyter-notebook 5.2.0
,bokeh
版本是0.12.9
和ipywidgets
是7.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)
现在,让我们创建一个柱状图中,还使用悬停工具:
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)
我使用一个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)
的问题是,我不能从部件获得一个更新图:
x.data_source = ColumnDataSource(update_source(df, myslider.value[0].date(), myslider.value[1].date()))
push_notebook(handle=h)
至少,它用的东西情节,因为hover
不能工作了...
我错过了什么?或者这是一个错误?
感谢所有帮助
马库斯
你应该试着让你现有的CDS改变它的'.data'属性,而不是一起取代CDS。这是一个更加行使的使用模式。 – bigreddot
好的,我尝试'newdf = update_source(df,myslider.value [0] .date(),myslider.value [1] .date())'和'x.data_source.data [“ind”] = newdf。 ind'和'给出'BokehUserWarning':'ColumnDataSource的列必须具有相同的长度。“并且图不会更新。我认为最基本的问题是,vbar的指数会减少一些......有没有一些工作的例子? – Markus
如果您使用'x.data_source = ColumnDataSource(newdf)',则不会引发BokehUserWarning。如果您进一步添加'fig.x_range.factors = newdf.ind'和'push_notebook(handle = h)',则绘图正确更新。但悬停不起作用。任何想法如何让悬停工作? – Markus