How to make 2D Graph animation in python using Jupyter Notebook

How to make 2D Graph animation in python using Jupyter Notebook

In this blog we are going to learn how to create 2D graph aniamtion


import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from IPython.display import HTML

# Set the figure and axis
fig, ax = plt.subplots()

# Set the x-axis range and the number of points
x = np.linspace(0, 2*np.pi, 100)

# Initialize a line object
line, = ax.plot(x, np.cos(x))

# Function to update the graph
def update(num, x, line):
    line.set_ydata(np.cos(x + num/10.0))  # update the data
    return line,

# Set up the animation
ani = animation.FuncAnimation(fig, update, frames=np.arange(0, 100), fargs=[x, line], interval=50)

# Display the animation
HTML(ani.to_jshtml())



Post a Comment

0 Comments