python中没有动画

2024-03-28 16:26:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用matplotlib.animation在python中制作一个动画情节,并且该包已经存在。但当我执行程序时,它没有显示任何错误,但也没有动画

%matplotlib notebook
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np

n=100
x= np.random.randn(n)
def update(curr):
if curr == n:
    a.event_source.stop()
    plt.cla()
    bins = np.arrange(-4,4,0.5)
    plt.hist(x[:curr],bins=bins)
    plt.axis([-4,4,0,30])
    plt.gca().set_title('Sampling the normal distribution')
    plt.gca().set_xlabel('Value')
    plt.gca().set_ylabel('Frequency')
    plt.annotate('n={}'.format(curr),[3,27])


    fig = plt.figure() 
    a = animation.FuncAnimation(fig,update,interval =100)

期待解决方案

谢谢你


Tags: importmatplotlibasnpfigupdate动画plt