Home > Enterprise >  Python: Animation Matplotlib error 'missing 1 required positional argument: 'curr'
Python: Animation Matplotlib error 'missing 1 required positional argument: 'curr'

Time:10-13

I am trying to animate 4 subplots that I set with GridSpec, and in which I want to represent 4 distributions that I set with random numbers, The code in which I set the subpots, the distributions and the function for the animation is the following:

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]

Then, I try to loop using the animation.FuncAnimation function with the following:

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

But I get the following error:

--------------------------------------------------------------------------- TypeError Traceback (most recent call last) in () 2 3 for i in range(3): ----> 4 a = animation.FuncAnimation(fig, anim(subplot = subplots[i], dist = dists[i]))

TypeError: anim() missing 1 required positional argument: 'curr'

Why is it asking me to specify curr? I thought the FuncAnimation function was able to understand itself that that's the argument to loop through. I think I am not understanding well the functioning of the animation module

NOTE: I have to say that I tried a similar code to this one, but just for un plot, without any grid, and without looping. In that case, I didn't have to specify subplot nor dist arguments within anim function because it wasn't necessary to include them when defining the anim function, since as I only had to pass one plot I just specify those values within the function, in that case, I didn't get any error, but now that I have to specify more arguments I am getting it.

CodePudding user response:

you have to call the function without argumrnts inside FUncAnimation like this:

a = animation.FuncAnimation(fig, anim)

so change your code like this to be able to do it :

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)

Also you have to change the anim function , according to the docs anim should be a function that return a sequence of Artist objects. In your case anim does not return anything. so you have to fixe it

  • Related