- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 line plot in Matplotlib?
To animate the line plot in matplotlib, we can take the following steps −
Create a figure and a set of subplots using subplots() method.
Limit x and y axes scale.
Create x and t data points using numpy.
Return coordinate matrices from coordinate vectors, X2 and T2.
Plot a line with x and F data points using plot() method.
To make animation plot, update y data.
Make an animation by repeatedly calling a function *func*, current fig, animate, and interval.
To display the figure, use show() method.
Example
import numpy as np from matplotlib import pyplot as plt, animation plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax = plt.subplots() ax.set(xlim=(-3, 3), ylim=(-1, 1)) x = np.linspace(-3, 3, 91) t = np.linspace(1, 25, 30) X2, T2 = np.meshgrid(x, t) sinT2 = np.sin(2 * np.pi * T2 / T2.max()) F = 0.9 * sinT2 * np.sinc(X2 * (1 + sinT2)) line, = ax.plot(x, F[0, :], color='k', lw=2) def animate(i): line.set_ydata(F[i, :]) anim = animation.FuncAnimation(fig, animate, interval=100, frames=len(t) - 1) anim.save('503.gif') plt.show()
Output
When we execute this code, it will display a line plot with animation.
- Related Articles
- How to animate a scatter plot in Matplotlib?
- How to plot a gradient color line in matplotlib?
- How to plot a 3D continuous line in Matplotlib?
- How to plot a smooth line with matplotlib?
- How to animate a pcolormesh in Matplotlib?
- How to plot a line graph from histogram data in Matplotlib?
- How to plot a dashed line on a Seaborn lineplot in Matplotlib?
- How to animate text in Matplotlib?
- How to animate a sine curve in Matplotlib?
- How to draw an average line for a scatter plot in MatPlotLib?
- How to plot a single line in Matplotlib that continuously changes color?
- Line plot with arrows in Matplotlib
- How to plot a multi-colored line, like a rainbow using Matplotlib?
- How to animate 3D plot_surface in Matplotlib?
- How to animate the colorbar in Matplotlib?

Advertisements