1
我试图在样本大小增加(从100到1000)时使形状的动画呈指数分布。还有一个滑块来配置分布的lambda参数和一个按钮来启动动画。动画无法在matplotlib上运行
我的问题是,即使布局得到渲染,当我按下开始按钮时,不会出现动画。这里是我的尝试至今:
Editted把事情简单化,我在Python笔记本上运行它:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.widgets import Slider
n0 = 100
n1 = 1000
#Create figure object
fig, ax = plt.subplots(1,1)
plt.subplots_adjust(bottom=0.3)
#Create Sliders for parameters
axlam = plt.axes([0.20, 0.05, 0.20, 0.03], facecolor=axcolor)
slam = Slider(axlam, 'Exp lambda', 0, 5, valinit=1)
#Default distributions
dexp = np.random.exponential(1, n1)
#Updates distributions when sliders are changed
def updateDist(val):
lam = slam.val
dexp = np.random.exponential(lam, n1)
slam.on_changed(updateDist)
#updates plot on FuncAnimation call
def update(curr):
if(curr == (n1-n0)):
a.event.source.stop()
plt.cla()
bins_exp = np.arange(0,4/slam.val,0.25)
ax.hist(dexp[:(n0+curr)],bins=bins_exp)
ax.set_title('Exp n={}'.format(n0+curr))
#Define Start button and function to start animation
goax = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(goax, 'Start', color=axcolor, hovercolor='0.975')
a = None
def start(event):
a = animation.FuncAnimation(fig,update,interval=100)
button.on_clicked(start)