如何使用ipywidgets SelectionRangeSlider更新holoviews Bars?

时间:2017-10-15 00:28:22

标签: jupyter-notebook ipywidgets holoviews

我想通过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)

这给出了测试DataFrame dfenter image description here

让我们对数据进行分组:

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)

enter image description here

现在,创建用于过滤和更新条形图的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)

enter image description here

(我已经在github上创建了一个问题,因为滑块没有正确显示标签,https://github.com/jupyter-widgets/ipywidgets/issues/1759

持续存在的问题:

  • 首次更新后图片宽度和尺寸会折叠为默认值(有没有办法让%%opts作为hv.Bars的参数?)

  • y-Scale应保持不变(即所有更新的0到150)

  • 是否有任何关于更新速度的优化?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

使用bokehhttps://github.com/bokeh/bokeh/issues/7082

了解如何执行此操作