代码之家  ›  专栏  ›  技术社区  ›  Mauro

Python:Animation Matplotlib错误“缺少1个必需的位置参数:”curr“”

  •  1
  • Mauro  · 技术社区  · 4 年前

    我正在尝试动画化我用GridSpec设置的4个子图,其中我想表示我用随机数设置的4个分布。我在其中设置子图、分布和动画函数的代码如下:

    import matplotlib
    import matplotlib.animation as animation
    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    
    #Create the figure and set the grid:
    fig = plt.figure()
    gspec = matplotlib.gridspec.GridSpec(6,6)
    top_left = plt.subplot(gspec[0:2,0:3])
    top_right = plt.subplot(gspec[0:2,3:], sharey = top_left)
    bottom_left = plt.subplot(gspec[3:5,0:3], sharey = top_left)
    bottom_right = plt.subplot(gspec[3:5,3:], sharey = top_left)
    
    #Create the random variables:
    
    x1 = np.random.normal(-2.5, 1, 10000)
    x2 = np.random.gamma(2, 1.5, 10000)
    x3 = np.random.exponential(2, 10000)+7
    x4 = np.random.uniform(14,20, 10000)
    
    #Create the function:
    
    n = 10000-100
    
    def anim(curr, subplot = None, dist = None):
        if curr >= 100:
            if curr == n+100:
                a.event_source.stop()
            subplot.cla()
            bins = np.arange(p1,p2,step)
            subplot.hist(dist[:curr], bins = bins)
            subplot.gca().set_ylabel('Frequency')
            subplot.gca().set_xlabel('Value')
            subplot.annotate('n={}'.format(curr+100))
    
    #Create the necessary lists for looping:
        
    subplots = [top_left, top_right, bottom_left, bottom_right]
    bins = [np.arange(-6,2,0.5),np.arange(-1,15,0.5), np.arange(5,20,0.5), np.arange(13.5,20.5,0.5)]
    dists = [x2,x2,x3,x4]
    

    然后,我尝试使用循环 animation.FuncAnimation 具有以下功能:

    for i in range(3):
        a = animation.FuncAnimation(fig, anim(subplot = subplots[i], dist = dists[i]))
    

    但我得到以下错误:

    ---------------------------------------------------------------------------TypeError Traceback(最近的调用 最后)在() 2. 对于范围(3)中的i为3: ---->4 a=动画。FuncAnimation(图,anim(subplot=subplot[i],dist=dists[i]))

    TypeError:anim()缺少1个必需的位置参数:“curr”

    为什么它要求我具体说明 curr ? 我认为 FuncAnimation 函数能够理解自己,这就是要循环通过的参数。我想我不太了解 animation 单元

    注意:我不得不说,我尝试了与此类似的代码,但只是针对一个单独的绘图,没有任何网格,也没有循环。在这种情况下,我不需要具体说明 subplot 也没有 dist anim函数中的参数,因为在定义 anim 函数,因为我只需要传递一个图,所以我只需要在函数中指定这些值,在这种情况下,我没有得到任何错误,但现在我必须指定更多的参数,我得到了它。

    1 回复  |  直到 4 年前
        1
  •  1
  •   sbabti zied    4 年前

    您必须在FUncAnimation中调用不带自变量的函数,如下所示:

    a = animation.FuncAnimation(fig, anim)
    

    所以,像这样更改您的代码,以便能够做到这一点:

    subplots = [top_left, top_right, bottom_left, bottom_right]
    bins = [np.arange(-6,2,0.5),np.arange(-1,15,0.5), np.arange(5,20,0.5), np.arange(13.5,20.5,0.5)]
    dists = [x2,x2,x3,x4]
    
    n = 10000-100
    
    def anim(curr):
        for i in range(3):
            if curr >= 100:
                if curr == n+100:
                    a.event_source.stop()
                subplots[i].cla()
                subplots[i].hist(dists[i][:curr], bins = bins[i])
                subplots[i].gca().set_ylabel('Frequency')
                subplots[i].gca().set_xlabel('Value')
                subplots[i].annotate('n={}'.format(curr+100))
    
    a = animation.FuncAnimation(fig, anim)
    

    此外,你还必须更改anim函数,根据文档,anim应该是一个返回艺术家对象序列的函数。在你的情况下,anim不会返回任何东西。所以你必须把它修好