Passing arguments in animation.FuncAnimation()


Question

How to pass arguments to animation() function? , I tried but dint worked. prototype of animation.FuncAnimation() is

class matplotlib.animation.FuncAnimation(fig, func, frames=None, init_func=None, fargs=None, save_count=None, **kwargs) Bases: matplotlib.animation.TimedAnimation

I have pasted code below , which changes i have to make?

import matplotlib.pyplot as plt
import matplotlib.animation as animation

def animate(i,argu):
    print argu

    graph_data = open('example.txt','r').read()
    lines = graph_data.split('\n')
    xs = []
    ys = []
    for line in lines:
        if len(line) > 1:
            x, y = line.split(',')
            xs.append(x)
            ys.append(y)
        ax1.clear()
        ax1.plot(xs, ys)
        plt.grid()

ani = animation.FuncAnimation(fig,animate,fargs = 5,interval = 100)
plt.show()
1
4
5/9/2016 8:59:38 AM

Accepted Answer

Check this simple example:

# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt 
import matplotlib.animation as animation
import numpy as np

data = np.loadtxt("example.txt", delimiter=",")
x = data[:,0]
y = data[:,1]

fig = plt.figure()
ax = fig.add_subplot(111)
line, = ax.plot([],[], '-')
line2, = ax.plot([],[],'--')
ax.set_xlim(np.min(x), np.max(x))
ax.set_ylim(np.min(y), np.max(y))

def animate(i,factor):
    line.set_xdata(x[:i])
    line.set_ydata(y[:i])
    line2.set_xdata(x[:i])
    line2.set_ydata(factor*y[:i])
    return line,line2

K = 0.75 # any factor 
ani = animation.FuncAnimation(fig, animate, frames=len(x), fargs=(K,),
                              interval=100, blit=True)
plt.show()

First, for data handling is recommended to use NumPy, is simplest read and write data.

Isn't necessary that you use the "plot" function in each animation step, instead use the set_xdata and set_ydata methods for update data.

Also reviews examples of the Matplotlib documentation: http://matplotlib.org/1.4.1/examples/animation/.

12
5/9/2016 4:46:58 PM

I think you're pretty much there, the following has a few minor tweaks basically you need to define a figure, use the axis handle and put fargs inside a list,

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

fig, ax1 = plt.subplots(1,1)

def animate(i,argu):
    print(i, argu)

    #graph_data = open('example.txt','r').read()
    graph_data = "1, 1 \n 2, 4 \n 3, 9 \n 4, 16 \n"
    lines = graph_data.split('\n')
    xs = []
    ys = []
    for line in lines:
        if len(line) > 1:
            x, y = line.split(',')
            xs.append(float(x))
            ys.append(float(y)+np.sin(2.*np.pi*i/10))
        ax1.clear()
        ax1.plot(xs, ys)
        plt.grid()

ani = animation.FuncAnimation(fig, animate, fargs=[5],interval = 100)
plt.show()

I replace example.txt with a hardwired string as I didn't have the file and added in a dependency on i so the plot moves.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon