- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to animate a sine curve in Matplotlib?
To make animated sine curve, we can take the following steps −
- Set the figure size and adjust the padding between and around the subplots.
- Create a new figure or activate an existing figure.
- Add an axes to the current figure and make it the current axes.
- Plot a line with empty lists.
- To initialize the line, pass empty lists.
- To animate the sine curve, update sine curve values and return the line instance.
- To display the figure, use show() method.
Example
import numpy as np from matplotlib import pyplot as plt from matplotlib import animation plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = plt.axes(xlim=(0, 2), ylim=(-2, 2)) line, = ax.plot([], [], lw=2) def init(): line.set_data([], []) return line, def animate(i): x = np.linspace(0, 2, 1000) y = np.sin(2 * np.pi * (x - 0.01 * i)) line.set_data(x, y) return line, anim = animation.FuncAnimation(fig, animate, init_func=init, frames=200, interval=20, blit=True) plt.show()
Output
- Related Articles
- How to plot sine curve on polar axes using Matplotlib?
- How to animate a pcolormesh in Matplotlib?
- How to animate text in Matplotlib?
- How to animate a scatter plot in Matplotlib?
- How to animate a line plot in Matplotlib?
- How to animate the colorbar in Matplotlib?
- How to animate 3D plot_surface in Matplotlib?
- How to add a cursor to a curve in Matplotlib?
- How to fill color below a curve in Matplotlib?
- How to curve text in a polar plot in matplotlib?
- How to remove a specific line or curve in Matplotlib?
- How can matplotlib be used to create a sine function in Python?
- How to fill rainbow color under a curve in Python Matplotlib?
- How to change the curve using radiobuttons in Matplotlib?
- How to animate a Seaborn heatmap or correlation matrix(Matplotlib)?

Advertisements