如何使用FuncAnimation(matplotlib,python)在同一个子图上为多个方程设置动画?

时间:2019-04-15 23:23:25

标签: python graph graphics jquery-animate

我想用相同的初始条件为同一系统的多个演化设置动画,以显示它们如何影响结果。

我有一个随时间变化的3变量系统,所以我制作了一个子图,在其中我对所有时间变化进行动画处理。现在,我想添加第二个动画,说明同一系统的演化,但是参数略有不同,因此我们可以真正看到它们如何以不同的方式演化。但是,我只能设法使它们在另一个之后出现,在此过程中先删除另一个。

到目前为止,我有

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_x.set_xlabel('t')
ax_x.set_ylabel('x')
ax_y.set_xlim(0, temps)
ax_y.set_ylim(-30, 30)
ax_y.set_xlabel('t')
ax_y.set_ylabel('y')
ax_z.set_xlim(0, temps)
ax_z.set_ylim(0, 50)
ax_z.set_xlabel('t')
ax_z.set_ylabel('z')

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()
figManager.window.showMaximized()
plt.show()

我想在每个子图中同时看到橙色和蓝色线条,但是只能让它们像这样怪异地振荡。

0 个答案:

没有答案