Animate two or more figures simultaneously with matplotlib

前端 未结 1 1278
孤街浪徒
孤街浪徒 2021-01-29 04:38

I\'m trying to animate two matplotlib figures at the same time. I wrote a script that creates two instances of matplotlib.animation.FuncAnimation, but only one of them animates,

1条回答
  •  南笙
    南笙 (楼主)
    2021-01-29 05:21

    Cause of Issue

    You must track each FuncAnimation object separately by holding reference to each individual object.

    In your for loop, the FuncAnimation object is being instantiated twice (with name anim), and hence in the namespace, effectively 'overwritten'.

    From the Animation Module Docs

    ...it is critical to keep a reference to the instance object. The animation is advanced by a timer ... which the [Func]Animation object holds the only reference to. If you do not hold a reference to the [Func]Animation object, it (and hence the timers), will be garbage collected which will stop the animation.

    Example Solution

    Instead, tracking both FuncAnimation objects in, for example a list of FuncAnimation objects, will allow them to both be animated with their own timers

    #...
    
    # List of Animation objects for tracking
    anim = []
    
    # Animate the figures
    for figcomps in [figcomps1,figcomps2]:
        fig,ax,line=figcomps
        anim.append(animation.FuncAnimation(fig,renderFrame,fargs=[line]))
    

    I wish to stress this is only an example, as there are many other methods of tracking multiple objects in a for loop, as explored in this question

    0 讨论(0)
提交回复
热议问题