Sin Wave Animation in Python

 Sin Wave Animation in Python


Code Link 

import matplotlib.pyplot as plt

import numpy as np

import matplotlib.animation as animation


# Set up the figure and axis

fig = plt.figure(figsize=(9, 16))

ax = plt.axes(xlim=(-5, 5), ylim=(-5, 5))


# Set up the graph title and labels for x and y axes

ax.set_title('Sine Wave Animation')

ax.set_xlabel('Time')

ax.set_ylabel('Amplitude')


# Create the line object

line, = ax.plot([], [], lw=2)


# Set up the animation function

def animate(i):

    x = np.linspace(-5, 5, 1000)

    y = np.sin(2 * np.pi * (x - 0.01 * i))

    line.set_data(x, y)

    return line,


# Create the animation

anim = animation.FuncAnimation(fig, animate, frames=200, interval=20)


# Save the animation as an mp4 file

Writer = animation.writers['ffmpeg']

writer = Writer(fps=30, metadata=dict(artist='Me'), bitrate=1800)

anim.save('graph_animation.mp4', writer=writer)






Post a Comment

0 Comments