【发布时间】:2021-04-03 12:32:45
【问题描述】:
我想创建一个带有多个下拉框的输出小部件和一个带有 Seaborn 的绘图,如下所示。 目的是使用不同的下拉框来选择变量,并根据用户输入输出不同的 Seaborn 图。
这里是代码。它有效,但不如预期。
dd = wd.Dropdown(
options=["YlGnBu","Blues","BuPu","Greens"],
value="Blues",
description='chosse cmap:',
disabled=False,
)
myout = wd.Output()
def draw_plot(change):
with myout:
myout.clear_output()
print('plotting out the following: sns.heatmap(df.corr(), annot=True, cmap = "YlGnBu")')
plt.figure(figsize=(8, 3))
sns.heatmap(df.corr(), annot=True, cmap = change.new)
plt.title("Correlation matrix");
dd.observe(draw_plot, names='value')
display(dd)
display(myout)
上述代码不会在每次选择一个新的保管箱变量时清除输出小部件,并添加 Seaborn 图。
我看到了这些解决方案: Stop seaborn plotting multiple figures on top of one another
根据我的意见,这并不令人满意。我想每次在输出小部件中显示一个新图形,即完全清除内容然后再次添加内容;再次阴谋。 我不明白为什么 clear_output() 清除文本行而不清除图形。
其次,正如上述链接中的一些答案所指出的,如果与 Seaborn 合作,我不想求助于底层库,即 Matplotlib。我认为这是一种解决方法。
那么正确的方法是什么?
谢谢
【问题讨论】:
标签: python seaborn ipywidgets