Let’s look at an instance of plotting an animated graph.
Let’s plot the trajectory of motion of an object that is moving at constant acceleration / deceleration.
The distance - time graph of an object moving at a constant acceleration has a parabolic shape. For example, I have given a graph, here.
Now let us create and animate a distance- time graph for a constant acceleration, using a sequence of time values. Before plotting this graph, we need to set a couple variable ready, for the calculation of distance (Using Newton’s equation).
Importing the modules
The important modules to perform this programing task are given below.
pyplot class from the matplotlib library
FuncAnimation class from the matplotlib.animation
numpy library
Creating instances of figure and axes
Now we need to create a figure and axes, to draw artists and set data, respectively.
Generating the time values
We can generate a set of time values in the form of an array. There are two ways by which we can generate a sequence of time values: 1. using the np.arange() function, and 2. np.linspace() function. If we use np.linspace(), we will have a definite number of time values between a specified time range. On the contrary, if we use np.arange(), there will be a definite number of values at a constant interval. For the time being, I have used the np.arange() , and the array of time values is set to a variable.
Defining the variables and the calculation of distances
We need to create variables for acceleration/ deceleration, and the Initial velocity. We will be drawing a line plot for a trajectory, for a specific initial velocity, and a scatter plot for the trajectory, for a different initial velocity.
We will be calculating the distance travelled, under a constant acceleration/ deceleration, using the following equation.
s = ut + 1/2*at**2
Drawing the initial figure for line and scatter plot
Now let’s draw initial figures for line plot and scatter plot. The line plot is drawn using the Axes.plot() method and scatter plot is drawn using the Axes.scatter method. Axes.plot() returns a matplotlib.lines.Line2Dobject and Axes.scatter returns a matplotlib.collections.PathCollectionsobject.
Here we have set an initial value for the x-data and y-data. Instead of this, we can also set an empty list. For the scatter plot, the other parameters determine the property of the plot: c is for color of the marker, s is for the size of the marker, label will be used for the legends.
Setting the x-limit and y-limit
We can set the x-limit and y-limit by providing a list of lower-bound and upper-bound. Optionally we can also set the x-label and the y-label.
Creating a FuncAnimation object
Now, let’s create a FuncAnimation object, and pass in the following parameters.
fig: The argument should be the figure instance on which the artists (line2D or pathCollection) are drawn.
func: A function to modify the data for each each frame. The function returns the artist.
frames: This determines the length of the animation. For each frame, a sequential subset of data will be used for drawing.
interval : This is the duration in milliseconds between the drawing of two consecutive frames.
Defining the func(function) parameter
The func parameter allows us to modify the data and return corresponding artist to be drawn on the figure. The figure is updated with the modified data for each frame. For a Line2D artist, the figure is updated using the set_data method. For a pathCollection artist, that draws scatter plot on the figure, the data are updated using the set_offsets method.
Comments