Wednesday 31 March 2021

Animated interactive vibrating string using python

I would like to plot an animated vibrating string using python but to be able to play it and to control the parameters used during the vibration (much like this Desmos calculation). So far, this is my code:

from __future__ import print_function
import numpy as np
import matplotlib.pyplot as plt
from ipywidgets import interact, interactive, fixed, interact_manual
import ipywidgets as widgets
import matplotlib as mpl

%matplotlib inline

def f(n=1, v=0.2, L=2, t=0):
    x = np.linspace(0, L, 2001)
    func = np.sin((n*np.pi*x)/L)*np.cos((n*np.pi*v*t)/L)

    
    plt.figure(figsize=(6,6))
    ax1 = plt.plot(x, func)
    plt.show()

interactive_plot = interactive(f, n=(0, 10, 1), v=(0.2, 5, 0.1), L=(0.2, 2, 0.1), t=(0, 10, 1))
output = interactive_plot.children[-1]
interactive_plot

I can control the wavefunction and all parameters, but I am not sure about what is the easiest way to animate it.

So far, I know that matplotlib can do it, but I am wondering if we have a more straightforward way to do animated interactive plots (using another package, maybe?).

Thanks in advance for any help.



from Animated interactive vibrating string using python

No comments:

Post a Comment