栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

动画在Matplotlib上不起作用

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

动画在Matplotlib上不起作用

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

考虑

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

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

import numpy as npimport matplotlib.pyplot as pltimport matplotlib.animation as animationfrom matplotlib.widgets import Slider, Buttonn0 = 100n1 = 1000#Create figure objectfig, ax = plt.subplots(1,1)plt.subplots_adjust(bottom=0.3)#Create Sliders for parametersaxcolor = "skyblue"axlam = plt.axes([0.20, 0.05, 0.20, 0.03], facecolor=axcolor)slam = Slider(axlam, 'Exp lambda', 0, 5, valinit=1)#Default distributionsdexp = [np.random.exponential(1, n1)]##Updates distributions when sliders are changeddef updateDist(val):    lam = slam.val    dexp[0] = np.random.exponential(lam, n1)slam.on_changed(updateDist)#updates plot on FuncAnimation calldef 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 animationgoax = 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()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/647410.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号