0

I have an animation working well with FuncAnimation. My goal is to change a parameter with a slider and update the animation instantly. I don't know how to combine both. You'll find my code down here, I call additionary function from an another file when you get psi_i,psi_r,psi_t = psi_step(k,x,V0). These 3 array are related to the value of V0 or what I called "newstep".

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider,Button,RadioButtons
from matplotlib.animation import FuncAnimation
from paquet_onde_v_17_03 import V0,k,Ak,Ek,psi_libre,evolution,psi_step,psi_i,psi_r,psi_t
#------------------------------------------------------------------------------
# initialisation du graphe et définition des limites d'affichage
fig, ax = plt.subplots(figsize=(15,8))
plt.subplots_adjust(bottom=0.25, left=0.05, right=0.95)
ax.set(xlim=(-2, 2), ylim=(-17, 17))
line, = ax.plot([], [])
line1,= ax.plot([], [])
line2,= ax.plot([], [])
line3,= ax.plot([], [])
plt.grid(True)

# Espace des k :
k0 = 100 
E0 = 1.5*k0**2
sigma_k = 5
delta_k = 1

# Espace des x :
xmin = -2
xmax = 2
N = 1500
x = np.linspace(xmin,xmax,N)
x1 = np.linspace(xmin,0,int(N/2))
x2 = np.linspace(0,xmax,int(N/2))

# initialisation des frames de l'animation
def init():
    line.set_data([], [])
    line1.set_data([], [])
    line2.set_data([], [])
    line3.set_data([], [])
    return line,line1,line2,line3

# fonction d'animation avec i le nombre de frames
T = 2e-4
t0=0.029
def animate(i):
    y1 = evolution(psi_i,Ak,Ek,i*T-t0) 
    y2 = evolution(psi_r,Ak,Ek,i*T-t0) 
    y3 = evolution(psi_t,Ak,Ek,i*T-t0) 
    line1.set_data(x1,y1)
    line2.set_data(x1,y2)
    line3.set_data(x2,y3)
    return line,line1,line2

anim = FuncAnimation(fig, animate, init_func=init, frames=600, interval=10,cache_frame_data=True)

# création de la fonction marche de potentiel
scale=5.5e-4
def V(x,V0):
    return scale*V0*(np.sign(x)+1)
plotstep, = plt.plot(x, V(x,V0), '--', color='k')

ax_newstep = fig.add_axes([0.1, 0.14, 0.65, 0.03])
s_newstep = Slider(ax_newstep, 'V0', 0.0, 8.3, valinit=scale*V0)

def update_plot_step(val):
    plotstep.set_ydata(val*(np.sign(x) + 1))
    fig.canvas.draw_idle()
    
s_newstep.on_changed(update_plot_step)
plt.show()

By slide the slider, I want to get the new value, calculate the new array psi_i, psi_t, psi_r. Then calculate the evolution of these and finaly animate it.

Malo
  • 13
  • 3
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Blue Robin Mar 22 '23 at 14:07
  • **EDIT** I find my problem, you just have to add the variables `psi_i,psi_t,psi_r,anim` as global variables in the function `update_plot_step`. – Malo Mar 23 '23 at 15:12
  • Global variables are almost never good and can cause problems – Blue Robin Mar 23 '23 at 15:45

0 Answers0