2

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

Daniel Goldfarb
  • 6,937
  • 5
  • 29
  • 61

1 Answers1

3

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:

  1. 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.
  2. 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:

enter image description here

Daniel Goldfarb
  • 6,937
  • 5
  • 29
  • 61
  • 1
    Hi Daniel, Am I right that the second approach could not be saved to GIF via animation.save(), because ani2.save() will override the ani1.save() result? Is there any workaround for this to save multiple animations with different intervals to 1 GIF? Thank you! – muted_buddy Jun 24 '23 at 00:49
  • @muted_buddy Yes, I think that is correct, or at least it seems so to me, that with the second approach one would have to, at least initially, save two GIFs. Perhaps there is a way to combine them side-by-side, for example see item number 2 on this page https://ezgif.com/help/merging-gifs (I am not endorsing that product, nor suggesting that there aren't others similar; I only did a google search on combining GIFs and referenced the first I found that said it could be done side-by-side.) – Daniel Goldfarb Jun 25 '23 at 11:47