Using AnimatPlot for Animating Graphs & Plots

Data Visualization helps in understanding different patterns, associations, visual insights from the data, etc. It is important because it uncovers the mystery behind the data tables in form of charts, graphs, and plots. There are tons of python libraries that help in visualizing the data like Matplotlib, Seaborn, etc.

AnimatPlot is an open-source python library that is built on top of Matplotlib and is used for creating highly interactive animated plots. Now, we will explore some of the functionalities that AnimatPlot provides.

Installing required libraries

We will start by installing AnimatPlot using pip. The command given below will do that.

pip install animatplot

Animatplot is built on the concept of blocks. We’ll start by animating a Line block.

First we need some imports.

Interactivity is not available in the static docs. Run the code locally to get interactivity.

So, we will use JupyterLab for our simulations.

We will import the required libraries for creating animated plots.

We will animate the function:

\(y=\cos (2 \pi(x+t)) \text { over the range } x=[0,1] \text {, and } t=[0,1]\)

Let’s generate the data:

In order to tell animatplot how to animate the data, we must pass it into a block. By default, the Line block will consider each of the rows in a 2D array to be a line at a different point in time.

We then pass a list of all our blocks into an Animation, and show the animation.

We’ll use the same data to make a new animation with interactive controls.

block = amp.blocks.Line(X, Y)
anim = amp.Animation([block])

anim.controls() # creates a timeline_slider and a play/pause toggle
anim.save_gif('images/line2') # save animation for docs
plt.show()

Displaying the Time

The above animation didn’t display the time properly because we didn’t tell animatplot what the values of time are. Instead it displayed the frame number. We can simply pass our values of time into our call to Animation.

block = amp.blocks.Line(X, Y)
anim = amp.Animation([block], t) # pass in the time values

anim.controls()
anim.save_gif('line3') # save animation for docs
plt.show()

Similarly, now we will add more data and create multiple plots in a single chart.

and once again:

Rispondi