我想通过SelectionRangeSlider
从Jupyter-notebook中的某些pandas DataFrame中选择数据,并使用全息视图条形图绘制过滤后的数据。
考虑以下示例:
import numpy as np
import pandas as pd
import datetime
import holoviews as hv
hv.extension('bokeh')
import ipywidgets as widgets
start = int(datetime.datetime(2017,1,1).strftime("%s"))
end = int(datetime.datetime(2017,12,31).strftime("%s"))
size = 100
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['who']= np.random.choice(['John', 'Paul', 'George', 'Ringo'], 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(2)
让我们对数据进行分组:
data = pd.DataFrame(df.groupby('who')['bytes'].sum())
data.reset_index(level=0, inplace=True)
data.sort_values(by="bytes", inplace=True)
data.head(2)
现在,创建用于过滤和更新条形图的SelectionRangeSlider
。
%%opts Bars [width=800 height=400 tools=['hover']]
def view2(v):
x = df[(df.zeit > r2.value[0].date()) & (df.zeit < r2.value[1].date())]
data = pd.DataFrame(x.groupby('who')['bytes'].sum())
data.sort_values(by="bytes", inplace=True)
data.reset_index(inplace=True)
display(hv.Bars(data, kdims=['who'], vdims=['bytes']))
r2 = widgets.SelectionRangeSlider(options = options, index = index, description = 'Test')
widgets.interactive(view2, v=r2)
(我已经在github上创建了一个问题,因为滑块没有正确显示标签,https://github.com/jupyter-widgets/ipywidgets/issues/1759)
持续存在的问题:
首次更新后图片宽度和尺寸会折叠为默认值(有没有办法让%%opts
作为hv.Bars
的参数?)
y-Scale应保持不变(即所有更新的0到150)
是否有任何关于更新速度的优化?
感谢您的帮助。