'Matplotlib (or mplfinance) two animation.FuncAnimation with different intervals
In python matplotlib finance Is it possible to have two different figures with animation.FuncAnimation in mplfinance where one has 12 axis with different style and another figure has two planes one for bar and another for volume. Another reason is that both figures having different intervals for refresh
Solution 1:[1]
There are two ways that I can think of to do this. I will show examples using simple matplotlib plots so as to focus on the animation function(s). If you understand the mplfinance animation example then you will be able make the analogous changes to make this work with mplfinance.
The two approaches are:
- Maintain more than one plot with a single func animation. If different update frequencies are needed, use modulo to update one or more of the plots. The disadvantage here is that the update period of each plot must be some multiple of the update period of the fastest plot.
- Create two func animations. This requires that each func animation be assigned to a different variable (and each such variable must remain in scope, i.e. not be deleted or destroyed, for the duration of animation).
Approach 1 Example: Single Func Animation maintaining two plots:
"""
A simple example of TWO curves from one func animation
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2,sharey=ax1)
x = np.arange(0, 2*np.pi, 0.01)
line1, = ax1.plot(x, np.sin(x))
line2, = ax2.plot(x, 0.5*np.sin(2.5*(x)))
def animate(i):
line2.set_ydata(0.5*np.sin(2.5*(x + i/5.0))) # update the data
if i%3 == 0: # modulo: update line1 only every third call
line1.set_ydata(np.sin(x + i/10.0)) # update the data
return line1,line2
ani1 = animation.FuncAnimation(fig, animate, np.arange(1, 200), interval=250)
plt.show()
Approach 2 Example: Two Func Animations maintaining two plots:
"""
A simple example of TWO animated plots
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2,sharey=ax1)
x = np.arange(0, 2*np.pi, 0.01)
line1, = ax1.plot(x, np.sin(x))
line2, = ax2.plot(x, 0.5*np.sin(2.5*(x)))
def animate1(i):
line1.set_ydata(np.sin(x + i/10.0)) # update the data
return line1,
def animate2(i):
line2.set_ydata(0.5*np.sin(2.5*(x + i/5.0))) # update the data
return line2,
ani1 = animation.FuncAnimation(fig, animate1, np.arange(1, 200), interval=250)
ani2 = animation.FuncAnimation(fig, animate2, np.arange(1, 200), interval=75)
plt.show()
If you create a script with either of the above examples and run under python (but do NOT run in a notebook or IDE, because that may or may not work) both cases should give an animation that looks something like this:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

