Table of contents
- Tutorial Video
- Creating a Line Plot with No Data
- Function set_data
- Function pause
- Manually creating an Animated Plot
- Using a For Loop
- Defining Custom Functions init and animate
- Using a While Loop to Loop Forever
- Saving an Animation to a GIF or Video File
- Adding More Frames
- Animated Scatter Plot
Let’s start by loading the required perquisites.
Now let’s generate x and y vectors and specify a scalar variable sec as 1.
We can now go ahead and setup a plot.
We then get a blank canvas:
Creating a Line Plot with No Data
Next we are going to create a line plot with no data.
Because there is no data plotted, the figure appears unchanged.
Note in line 22, we use [line] and not line:
There is a subtle difference as the plot function can be used with multiple lines:
It is possible to modify the code above (line 15) to create a plot with no data. This is done in line 15. Note the ax is setup with the xlim and ylim in line 11 meaning the axis limits won’t change with this plot.
Once a plot has been created we may use the set_data function to change the data. In this case, let’s set the new data to be blank.
We can then go ahead and save this figure.
There is a function within the matplotlib library called pause, which will pause the script file and the chart for a specified time interval. We can use this after creating the blank figure.
Manually creating an Animated Plot
After this pause, it is possible to use set_data to update the data with new data. In this case we can assign the data to be to the 1st point (zero order indexing, meaning we are starting at 0 and going up in steps of 1 until the upper limit but don’t reach the upper limit, meaning only the point at the origin in this case).
The following will show:
The three png images will be created:
We can repeat the process until we get to the end of the line. In this case we index up to 11 because we index up to but not including the last point.
This gives the following animated line plot:
This gives the following png image files are created of each frame.
Using a For Loop
As you see however, we are repeating ourselves multiple times. This code can be simplified using a for loop.
Defining Custom Functions init and animate
Next it is possible to set two functions, an initialisation function init and an animate function animate. The function init will have no input arguments as it contains no data and the input argument for animate will be the frame number. Both will return an updated [line].
Once the functions are created, they will need to be called. init() as mentioned has no input arguments and is only called once. We can create a variable frame which is the length of the x data, x plus 1 (so we end up with the length of x plus 1 for zero indexing up to the maximum value plus 1 but not including the maximum value plus 1 i.e. so we index to the maximum value).
Using a While Loop to Loop Forever
So far our animated plays only once. If we want it to instead loop forever we can embed the calling of the functions init() and animate() within a while loop. Saving files are removed to prevent continuous saving over the same file name.
MatPlotLib has an inbuilt function called animation which can be used for animations. We need to import matplotlib.animation as anim (line 4). The function animation can then be used (line 38) it requires the figure variable name, the animate function and the init function as the first three input arguments. In this case instead of specifying a for loop, we specify the frames (the function will loop through these using the function animate). The interval is the frame rate in ms. The final input argument is blit, which can be used in some cases to save memory by not refreshing all elements on the graph which are constant (if you have issues set this to False)
Saving an Animation to a GIF or Video File
For this next part, ffmpeg must be installed. Close Spyder and go to start and open up the Anaconda Powershell Prompt:
Type in the following command.
When done close the Anaconda Powershell Prompt and relaunch Spyder:
Adding More Frames
A step can be specified (line 6) and the x and y data (line 7-8) can be updated using this step. This step can be used to modify the interval of a frame (line 44)and to amend the frames per second in the created video file (line 49-52).
Animated Scatter Plot
For a scatter plot line 23, 28, 29, 36 and 37 must be updated. Note the scatter plot can only have one x and y so scatter is used opposed to [line]. The function set_data only works for a line plot, for a scatter plot, the function set_offsets must be used instead (line 28 and line 36). This function expects the data to be input as a column, so the input data has to be enclosed in np.c_ when compared to the line data. Also blit doesn’t work for a scatter plot (line 46) so must be set to False.