我正在使用holoviews和panel开发一个小部件-它包括读取pandas.dataFrame并显示每列的曲线。我需要的交互是能够在绘图中添加/删除列。在我的实际用例中,有太多的列,所以我不能利用bokeh+holoviews已经提供的交互式图例。
我举了一个小例子,“有点像是工作”,但我可能做错了,因为每次与panel.widgets.MultiChoice进行交互时,我都会重新加载绘图数据(这显然是错误的)。
import holoviews as hv
import numpy as np
import pandas as pd
import colorcet as cc
import panel as pn
pn.extension()
hv.extension("bokeh")
# generate some data
def get_data():
data = {
"1998": np.random.rand(365),
"1999": np.random.rand(365),
"2000": np.random.rand(365),
"2002": np.random.rand(365),
"2003": np.random.rand(365),
}
df = pd.DataFrame(data, index=range(0, 365))
return df
# utility to help me placing the month label around the 2nd week of each month
def split_list(a, n):
k, m = divmod(len(a), n)
return list(
list(a[i * k + min(i, m) : (i + 1) * k + min(i + 1, m)]) for i in range(n)
)
def get_ticks(df, pos):
splitter = split_list(df.index, 12)
months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
]
xticks_map = [i for i in zip([splitter[i][pos] for i in range(0, 12)], months)]
return xticks_map
# plotting method
def get_mplot(df, cols=None):
if cols:
df = df[cols]
if len(df.columns) == 0:
print("No coumns selected")
return None
grid_style = {
"grid_line_color": "black",
"grid_line_width": 1.1,
"minor_ygrid_line_color": "lightgray",
"minor_xgrid_line_color": "lightgray",
"xgrid_line_dash": [4, 4],
}
colors = cc.glasbey_light[: len(list(df.columns))]
xticks_map = get_ticks(df, 15)
multi_curve = [
hv.Curve((df.index, df[v]), label=str(v)).opts(
xticks=xticks_map,
xrotation=45,
width=900,
height=400,
line_color=colors[i],
gridstyle=grid_style,
show_grid=True,
)
for i, v in enumerate(df)
]
mplot = hv.Overlay(multi_curve)
return mplot
# get the data
df = get_data()
# create a multi-choice widget
years = pn.widgets.MultiChoice(
name="Years", options=list(df.columns), margin=(0, 20, 0, 0)
)
# bind plot and multi-choice
@pn.depends(years)
def get_plot(years):
df = get_data()
if years:
df = df[years]
mplot = get_mplot(df, years)
return mplot
pn.Column("Plot!", get_plot, pn.Row(years), width_policy="max").servable()为了方便起见,我将代码在线存储为gist上的notebook:
我的问题是当我定义@pn.depends method时,在单元格#7 (在notebook中)的holoviews和panel之间的交互-到目前为止,我让它工作的唯一方法是在每个交互…上“重新加载”数据(cell_out:[#21],df = get_data()中的line [#3] ),如果数据开始增加,这显然会减慢整个应用程序的运行速度。
本质上,我需要一个与绘图组件交互的方法,而不是在每次交互时重新执行绘图。简单地说,我会写一个连接到绘图的处理程序,但我的理解是,在holoviews+panel (因为它们是构建在bokeh之上的一组更高级的库)中,应该有一种更简单的方法来实现同样的目的。
关于如何避免重新加载数据集,您有什么建议吗?
发布于 2021-03-03 19:24:42
我认为你只需要先加载数据,而不是覆盖数据帧,比如:
df = get_data()
@pn.depends(years)
def get_plot(years):
if years:
df1 = df[years]
mplot = get_mplot(df1, years)
return mplot发布于 2021-03-03 20:14:29
构建在@rich-signell之上,当从多项选择小部件中删除所有条目时,这也将起作用:
@pn.depends(years)
def get_plot(years):
if years:
df1 = df[years]
mplot = get_mplot(df1, years)
else:
mplot = get_mplot(df)
return mplot然而,我面临的问题是由于我如何在jupyter笔记本中原型化代码的方式。在jupyter笔记本上运行holoviews+panel时,有一个奇怪的行为。我可以用下面的版本在两个不同的jupyter服务器上复制它
Jupyterlab, holoviews, panel
'2.2.9', '1.14.0', '0.11.0a3.post2+g5aa0c91'
'3.0.7', '1.14.2.post2+gd235b1cb0','0.10.3'问题是,应用于用@pn.depends修饰的get_plot()方法的更改直到notebook内核重启后才被panel小部件接受,因此任何更改代码(也是有效的解决方案)的尝试都是无效的,并且使我感到困惑。
已尝试在此录制https://gist.github.com/epifanio/6c9827f9163de359130102a29d7f3079中显示问题
https://stackoverflow.com/questions/66445536
复制相似问题