使用 ipywidgets 更新图表:
update chart with ipywidgets:
我在 jupyter notebook 上使用 seaborn,想要一个滑块来更新图表。我的代码如下:
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()
来显示它,例如 answer 中的说明。
结合 %matplotlib inline
魔术命令,这对我来说很好用:
%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))
另一种解决方案是更新绘图数据而不是重新绘制新绘图,如此处解释:
我在 jupyter notebook 上使用 seaborn,想要一个滑块来更新图表。我的代码如下:
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()
来显示它,例如 answer 中的说明。
结合 %matplotlib inline
魔术命令,这对我来说很好用:
%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))
另一种解决方案是更新绘图数据而不是重新绘制新绘图,如此处解释: