# Python Animation Mastery: Crisp 1920x1080 Videos with Matplotlib

## Step 1: Importing the Necessary Libraries

To begin, we import the required libraries: NumPy for numerical operations, Matplotlib for creating visualizations, and specifically FuncAnimation from matplotlib.animation for generating animations.

Download ffmpeg from here

## Step 2: Setting Up the Figure and Axis

We create a Matplotlib figure and axis to house our animation. By setting the figure size in inches to 1920x1080 (the resolution of Full HD screens), we lay the groundwork for our high-quality animation.

## Step 3: Defining Data for the Graph

For this demonstration, we generate sample data—a series of x values evenly spaced between 0 and 10, and corresponding y values calculated as the sine of the x values.

Plotting the Initial Data

We plot the initial data on the axis—a sine wave in this case. We set labels for the x and y axes, provide a title for the graph, and establish axis limits.

## Step 4: Creating the Animation Function

To animate the graph, we define a function called animate that updates the y values of the sine wave for each frame. The y values are adjusted by a fraction of the frame number to create the appearance of animation. The function returns the updated line object.

## Step 5: Generating the Animation

Using FuncAnimation, we combine the figure, the animation function, and parameters like frames (total number of frames) and interval (time between frames in milliseconds) to create the animation. Setting blit=True improves animation performance.

## Step 6: Saving the Animation (Optional)

If desired, we can save the animation as a video file named 'sine_wave_animation.mp4'. We specify the desired frames per second (fps) for the output video using the fps parameter.

Displaying the Animation

Finally, we use plt.show() to display the animation within our environment. The animation showcases a smooth sine wave evolving over time, highlighting the potential of Matplotlib for creating engaging visualizations.

Code Link

import numpy as np

import matplotlib.pyplot as plt

from matplotlib.animation import FuncAnimation

# Step 1: Import necessary libraries

# Step 2: Create a figure and axis

fig, ax = plt.subplots()

fig.set_size_inches(1920/100, 1080/100)  # Set figure size in inches

# Step 3: Define the data for your graph

x = np.linspace(0, 10, 100)  # Sample x values

y = np.sin(x)  # Corresponding y values

# Plot the initial data

line, = ax.plot(x, y)

ax.set_xlim(0, 10)

ax.set_ylim(-1, 1)

ax.set_xlabel('X Axis Label')

ax.set_ylabel('Y Axis Label')

ax.set_title('Sine Wave Animation')

# Step 4: Create a function to update the graph for each frame

def animate(frame):

# Update the y values for the sine wave

line.set_ydata(np.sin(x + frame / 10))

return line,

# Step 5: Use FuncAnimation to create the animation

fps = 30  # frames per second

frames = 300  # total number of frames

animation = FuncAnimation(fig, animate, frames=frames, interval=1000/fps, blit=True)

# Step 6: Save the animation as a video file (optional)

animation.save('sine_wave_animation.mp4', writer='ffmpeg', fps=fps)

# Display the animation

plt.show()