Animated Line Plot

MatplotlibMatplotlibBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This tutorial will guide you through the process of creating an animated line plot using Python Matplotlib library. The line plot will display a sine wave with a changing amplitude over time.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/BasicConceptsGroup(["`Basic Concepts`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/PlottingDataGroup(["`Plotting Data`"]) matplotlib(("`Matplotlib`")) -.-> matplotlib/AdvancedTopicsGroup(["`Advanced Topics`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") matplotlib/BasicConceptsGroup -.-> matplotlib/importing_matplotlib("`Importing Matplotlib`") matplotlib/BasicConceptsGroup -.-> matplotlib/figures_axes("`Understanding Figures and Axes`") matplotlib/PlottingDataGroup -.-> matplotlib/line_plots("`Line Plots`") matplotlib/AdvancedTopicsGroup -.-> matplotlib/animation_creation("`Animation Creation`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_visualization("`Data Visualization`") subgraph Lab Skills python/comments -.-> lab-48928{{"`Animated Line Plot`"}} matplotlib/importing_matplotlib -.-> lab-48928{{"`Animated Line Plot`"}} matplotlib/figures_axes -.-> lab-48928{{"`Animated Line Plot`"}} matplotlib/line_plots -.-> lab-48928{{"`Animated Line Plot`"}} matplotlib/animation_creation -.-> lab-48928{{"`Animated Line Plot`"}} python/booleans -.-> lab-48928{{"`Animated Line Plot`"}} python/tuples -.-> lab-48928{{"`Animated Line Plot`"}} python/function_definition -.-> lab-48928{{"`Animated Line Plot`"}} python/importing_modules -.-> lab-48928{{"`Animated Line Plot`"}} python/numerical_computing -.-> lab-48928{{"`Animated Line Plot`"}} python/data_visualization -.-> lab-48928{{"`Animated Line Plot`"}} end

Import Libraries

The first step is to import the necessary libraries. We will be using Matplotlib for creating the plot and NumPy for generating data.

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation

Initialize the Figure and Axes

Next, we need to initialize the figure and axes for the plot. This can be done using the subplots() function from Matplotlib.

fig, ax = plt.subplots()

Generate Data

In this step, we will generate the data for the line plot. We will be using the NumPy arange() function to generate an array of values for the x-axis, and the sin() function to generate an array of y-axis values for a sine wave.

x = np.arange(0, 2*np.pi, 0.01)
line, = ax.plot(x, np.sin(x))

Define the Animation Function

The animation function will be called by the FuncAnimation() function and will be used to update the plot with new data. In this example, we will be updating the y-axis values of the line plot with a sine wave that has a changing amplitude over time.

def animate(i):
    line.set_ydata(np.sin(x + i / 50))  ## update the data.
    return line,

Create the Animation Object

Now we can create the animation object using the FuncAnimation() function. We will pass in the figure object, the animation function, the update interval, and the number of frames to be saved.

ani = animation.FuncAnimation(
    fig, animate, interval=20, blit=True, save_count=50)

Display the Plot

Finally, we can display the plot using the show() function from Matplotlib.

plt.show()

Summary

In this tutorial, we have learned how to create an animated line plot using Python Matplotlib library. We have initialized the figure and axes, generated data, defined the animation function, and created the animation object. We have then displayed the plot using the show() function.

Other Matplotlib Tutorials you may like