动画在Matplotlib上不起作用 [英] Animation not working on matplotlib

查看:65
本文介绍了动画在Matplotlib上不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使形状的动画随着样本大小的增加(从100增加到1000)而呈指数分布。还有一个用于配置发行版的lambda参数的滑块和一个用于启动动画的按钮。

I am trying to make an animation of the shape an exponential distribution as the sample size increases (from 100 to 1000). There is also a slider to configure the lambda parameter of the distribution and a button to start the animation.

我的问题是,即使布局得到渲染,当我按开始按钮没有动画发生。到目前为止,这是我的尝试:

My problem is that even though the layout gets rendered, when I press the start button no animation occurs. Here's my attempt so far:

为简化起见,我在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)


推荐答案

您应该注意函数的命名空间。

You should mind the namespaces of functions.

考虑

a = 0
def f():
    a = 1
f()
print(a) # will print 0

因此,您需要处理实际对象,而不是它们的某些本地副本。

So you need to work on the actual objects and not some local copy of them.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.widgets import Slider, Button

n0 = 100
n1 = 1000

#Create figure object
fig, ax = plt.subplots(1,1)
plt.subplots_adjust(bottom=0.3)

#Create Sliders for parameters
axcolor = "skyblue"
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[0] = 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()
    ax.clear()
    bins_exp = np.arange(0,4/slam.val,0.25)
    ax.hist(dexp[0][:(n0+curr)],bins=bins_exp)
    ax.set_title('Exp n={}'.format(n0+curr))
    fig.canvas.draw_idle()

#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 = [0]
def start(event):
    a[0] = animation.FuncAnimation(fig,update,interval=100)
button.on_clicked(start)

plt.show()

这篇关于动画在Matplotlib上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆