我正在研究一个带有holoviews
and的小部件panel
- 它包括读取 apandas.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()
为方便起见,我将代码作为笔记本在线存储在一个要点上:
我的问题是当我定义时单元格(在笔记本中)之间holoviews
的交互- 到目前为止,我让它工作的唯一方法是在每次交互时“重新加载”数据……(cell_out: , line , in )如果数据开始增加,这显然会减慢整个应用程序的速度。panel
#7
@pn.depends method
[#21]
[#3]
df = get_data()
本质上,我需要一种与绘图组件交互的方法,而不是在每次交互时重新执行绘图。在普通的散景中,我会编写一个连接到情节的处理程序,但我的理解是,在holoviews
+中panel
(因为它们是建立在散景之上的一组更高级别的库)应该有一种更简单的方法来实现相同的效果。
您对如何避免重新加载数据集有任何提示吗?