我正试图为仪表盘建立一个交互式绘图。数据在pandas数据框中 state_df
.
ty=['Confirmed','Bedridden','Recovered']
def type_tt(need_type):
need=[]
need.append(need_type)
my_df=state_df[state_df.Status.isin(need)]
my_df=my_df.set_index('Date')
return my_df
def heat_map(types):
num=10# 10 day
my_df=type_tt(types)
sns.heatmap((my_df.drop('Status',axis=1)).tail(num)[top_14],cmap='Blues', linewidths=0.1)
#df.style.background_gradient(cmap='Blues')
#heat_map(types_op)
app7=pn.interact(heat_map,types=ty)
app7
但是当我从下拉菜单中改变选项时,情节并没有改变。我试着查了一下链接的文档,但是没有任何效果。
有什么线索吗?
这里要补充的最重要的一点是你的函数 heat_map()
需要 return
你的情节。我想这就是缺少的东西。
因为没有样本数据,所以很难重现你的例子,但这里有一个例子可以用。我用了 hvplot 而不是seaborn来创建一个交互式热图。 示例代码。
# import libraries
import numpy as np
import pandas as pd
import hvplot.pandas
import panel as pn
pn.extension()
# create sample data
df = pd.DataFrame({
'date': pd.date_range(start='01-01-2020', end='31-12-2020'),
'status': np.random.choice(['confirmed', 'bedridden', 'recovered'], 366),
'status2': np.random.choice(['A', 'B', 'C'], 366),
'value': np.random.rand(366) * 100
})
types = ['confirmed', 'bedridden', 'recovered']
# you need to return your plot to get the interaction
def plot_heatmap(chosen_type):
df_selected = df[df['status']==chosen_type]
# hvplot is handy for creating interactive plots
heatmap = df_selected.hvplot.heatmap(x='date', y='status2', C='value')
return heatmap
# show your interactive plot with dropdown
pn.interact(plot_heatmap, chosen_type=types)
顺便说一下,用 hvplot 你不需要所有这些额外的代码来获得一个像样的交互式热图和下拉菜单。你可以直接做。
df.hvplot.heatmap(x='status2', y='date', C='value', groupby='status')
更多关于pn.interact()的信息。 https:/panel.holoviz.orggetting_startedindex.html。