2017-04-08 97 views
1

我用一个直方图的四个子图分别根据随机正态,伽马,指数和均匀分布制作了一个图。我用matplotlib和Jupyter笔记本做了它。这是一个通过ipywidgets lib的交互式图形。特别是,有四个滑动条控制每个直方图上的样本大小并相应地更新它们。但是,更新直方图时,它令人生厌地闪烁。有什么办法可以避免这种情况?谢谢。ipywidgets:使用交互时避免闪烁

现在的代码要在jupyter笔记本运行:

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 
%matplotlib notebook 
from ipywidgets import * 

n = 1000 
x1 = np.random.normal(-2.5, 1, n) 
x2 = np.random.gamma(2, 1.5, n) 
x3 = np.random.exponential(2, n)+7 
x4 = np.random.uniform(14,20, n) 
x = [x1, x2, x3, x4] 

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(10,7)) 
axs = [ax1,ax2,ax3,ax4] 

titles = ['x1\nNormal', 'x2\nGamma', 'x3\nExponential', 'x4\nUniform'] 
subplots_axes = [[-7,2,0,250], [0,4.5,0,250], [7,25,0,250], [14,20,0,250]] 

bins = [np.arange(-6, 6, 0.5), 
np.arange(0, 10, 0.5), 
np.arange(7, 17, 0.5), 
np.arange(14, 24, 0.5)] 

fig.subplots_adjust(hspace=0.5) 

def plt_dist(s, sample): 
    axs[s].hist(x[s][:sample], bins=bins[s], linewidth=0, color='#1F77B4') 
    axs[s].axis(subplots_axes[s]) 
    axs[s].set_title('{}'.format(titles[s])) 
    axs[s].set_ylabel('Frequency') 
    axs[s].set_xlabel('Value') 
    axs[s].annotate('n = {}'.format(sample), xycoords='axes fraction', xy = [0.8,0.9]) 
    display(fig) 

for s in range(0,4): 
    sld_bar = interact(plt_dist, s = fixed(s), sample = widgets.IntSlider(min=100,max=1000+45,step=1,value=100)) 

回答

1

这不是真的清楚什么display(fig)会做它的需要或什么。

对于我来说,去除线,并在plt_hist功能的开始,而不是清除轴(axs[s].clear())的作品只是罚款和“闪烁”是不存在了。

import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt 
%matplotlib notebook 
from ipywidgets import * 

n = 1000 
x1 = np.random.normal(-2.5, 1, n) 
x2 = np.random.gamma(2, 1.5, n) 
x3 = np.random.exponential(2, n)+7 
x4 = np.random.uniform(14,20, n) 
x = [x1, x2, x3, x4] 

fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(10,7)) 
axs = [ax1,ax2,ax3,ax4] 

titles = ['x1\nNormal', 'x2\nGamma', 'x3\nExponential', 'x4\nUniform'] 
subplots_axes = [[-7,2,0,250], [0,4.5,0,250], [7,25,0,250], [14,20,0,250]] 

bins = [np.arange(-6, 6, 0.5), 
np.arange(0, 10, 0.5), 
np.arange(7, 17, 0.5), 
np.arange(14, 24, 0.5)] 

fig.subplots_adjust(hspace=0.5) 

def plt_dist(s, sample): 
    axs[s].clear() # <-- clear axes 
    axs[s].hist(x[s][:sample], bins=bins[s], linewidth=0, color='#1F77B4') 
    axs[s].axis(subplots_axes[s]) 
    axs[s].set_title('{}'.format(titles[s])) 
    axs[s].set_ylabel('Frequency') 
    axs[s].set_xlabel('Value') 
    axs[s].annotate('n = {}'.format(sample), xycoords='axes fraction', xy = [0.8,0.9]) 
    #display(fig) <--- delete this 

for s in range(0,4): 
    sld_bar = interact(plt_dist, s = fixed(s), 
       sample = widgets.IntSlider(min=100,max=1000+45,step=1,value=100)) 
+0

我必须使用'display(fig)',因为我想用四个子图使用for循环更新一个图,就像在[这里](http://stackoverflow.com/questions/21360361/how -to-动态更新-A-情节在-A - 环 - 在-IPython的笔记本中之单细胞)。否则,该图不显示。我曾尝试过使用'plt.cla()',但它不起作用。 –

+0

那么,你可以从我的答案中运行脚本吗?根据是否有效,我们可以找到for-loops的解决方案。 – ImportanceOfBeingErnest

+0

是的,我可以。该解决方案没有问题。 –