Jupyter notebook 使用IPyWidget更新图表:

Jupyter notebook 使用IPyWidget更新图表:,jupyter-notebook,seaborn,ipywidgets,Jupyter Notebook,Seaborn,Ipywidgets,我正在使用seaborn on jupyter笔记本,希望使用滑块更新图表。我的代码如下: from ipywidgets import interact, interactive, fixed, interact_manual import numpy as np import seaborn as sns from IPython.display import clear_output def f(var): print(var) clear_output(wait=Tru

我正在使用seaborn on jupyter笔记本,希望使用滑块更新图表。我的代码如下:

from ipywidgets import interact, interactive, fixed, interact_manual
import numpy as np
import seaborn as sns
from IPython.display import clear_output

def f(var):
    print(var)
    clear_output(wait=True)
    sns.distplot(list(np.random.normal(1,var,1000)))

interact(f, var=10);

问题:每次移动滑块时,图形都会重复。如何更新图表?

Seaborn绘图应作为常规matplotlib绘图处理。因此,您需要使用
plt.show()
来显示它,如本例所述

结合
%matplotlib inline
magic命令,这对我来说很好:

%matplotlib inline
from ipywidgets import interact
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

def f(var):
    sns.distplot(np.random.normal(1, var, 1000))
    plt.show()
interact(f, var = (1,10))
另一个解决方案是更新绘图的数据,而不是重新绘制新的绘图,如下所述:

我认为这已经发生了,这将取决于您的IPyWidget版本。