I want to animate multiple evolutions of the same system with slightly different initial conditions to show how they affect the results.

I have a 3 variable system which all evolve in time, so I made a subplot in which I animate all of them with regards to time evolution. Now I want to add a second animation of the evolution of the same system, but with slightly different parameters so we can really see how they evolve differently. However, I can only manage to get them to sort of appear one after the other, deleting the other before it in the process.

So far I have

import numpy as np
from random import uniform
from scipy.integrate import odeint
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as anime

# Définit les constantes que Lorenz utilise
sigma = 10
b = 8 / 3
r = 28
delta_t = 0.01
temps = 100
nbIter = int(temps / delta_t)
t = np.linspace(0,temps, num=nbIter)

# Conditions initiales que Lorenz utilise
x_0 = 0
y_0 = 1
z_0 = 0
conditions_0 = [x_0, y_0, z_0]

x_1 = 0 + uniform(0, 0.1)
y_1 = 1 + uniform(0, 0.1)
z_1 = 0 + uniform(0, 0.1)
conditions_1 = [x_1, y_1, z_1]

def equations(variables, t):
    x, y, z = variables
    dxdt = sigma*(y - x)
    dydt = (-x*z) + (r*x) - y
    dzdt = (x*y) - (b*z)
    systemeEDO = [dxdt, dydt, dzdt]
    return systemeEDO

# Solve EDOs
solutions_0 = odeint(equations, conditions_0, t)
solutions_1 = odeint(equations, conditions_1, t)

# Plot les graphs x(t), y(t) et z(t) séparés
fig, (ax_x, ax_y, ax_z) = plt.subplots(3, 1)
line_x, = ax_x.plot([], [])
line_x1, = ax_x.plot([], [])
line_y, = ax_y.plot([], [])
line_y1, = ax_y.plot([], [])
line_z, = ax_z.plot([], [])
line_z1, = ax_z.plot([], [])
ax_x.set_xlim(0, temps)
ax_x.set_ylim(-30, 30)
ax_y.set_xlim(0, temps)
ax_y.set_ylim(-30, 30)
ax_z.set_xlim(0, temps)
ax_z.set_ylim(0, 50)

x_det = solutions_0[:, 0]
y_det = solutions_0[:, 1]
z_det = solutions_0[:, 2]
x_det1 = solutions_1[:, 0]
y_det1 = solutions_1[:, 1]
z_det1 = solutions_1[:, 2]

def init_x():
    line_x.set_data([], [])
    return line_x,

def animate_x(i):
    y = x_det[:i]
    val_t = t[:i]
    line_x.set_data(val_t, y)
    return line_x,

def init_x1():
    line_x1.set_data([], [])
    return line_x1,

def animate_x1(i):
    y = x_det1[:i]
    val_t = t[:i]
    line_x1.set_data(val_t, y)
    return line_x1,

def init_y():
    line_y.set_data([], [])
    return line_y,

def animate_y(i):
    y = y_det[:i]
    val_t = t[:i]
    line_y.set_data(val_t, y)
    return line_y,

def init_y1():
    line_y1.set_data([], [])
    return line_y1,

def animate_y1(i):
    y = y_det1[:i]
    val_t = t[:i]
    line_y1.set_data(val_t, y)
    return line_y1,

def init_z():
    line_z.set_data([], [])
    return line_z,

def animate_z(i):
    y = z_det[:i]
    val_t = t[:i]
    line_z.set_data(val_t, y)
    return line_z,

def init_z1():
    line_z1.set_data([], [])
    return line_z1,

def animate_z1(i):
    y = z_det1[:i]
    val_t = t[:i]
    line_z1.set_data(val_t, y)
    return line_z1,

anim_x = anime.FuncAnimation(fig, animate_x, init_func=init_x, frames=int(nbIter), interval=5, blit=True)
anim_x1 = anime.FuncAnimation(fig, animate_x1, init_func=init_x1, frames=int(nbIter), interval=5, blit=True)
anim_y = anime.FuncAnimation(fig, animate_y, init_func=init_y, frames=int(nbIter), interval=5, blit=True)
anim_y1 = anime.FuncAnimation(fig, animate_y1, init_func=init_y, frames=int(nbIter), interval=5, blit=True)
anim_z = anime.FuncAnimation(fig, animate_z, init_func=init_z, frames=int(nbIter), interval=5, blit=True)
anim_z1 = anime.FuncAnimation(fig, animate_z1, init_func=init_z, frames=int(nbIter), interval=5, blit=True)

figManager = plt.get_current_fig_manager()

I want to see the orange and blue lines at the same time in each subplot, but can only get them to oscillate weirdly like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.