Let’s plot a bar graph. Not a simple static bar graph, but the kind that is animated, as you see below.
Let’s see how we can plot this animated graph using the ‘FuncAnimation’ class, of the matplotlib library.
First of all, we need to import the necessary modules. We import pyplot class from the matplotlib module; animation class from the matplotlib module.
We will be using the FuncAnimation class of the animation base class, to create the animation.
Next, we need to create an instance of a figure and Axes to be placed on the figure.
Once the figure and Axes have been created, next we need to draw artists on the figure for bar plots. The artist for bar plot is drawn using the matplotlib.pyplot.bar() method. Basically, and essentially, we need to provide two arrayLike data to matplotlib.pyplot.bar(), that correspond to the x-coordinates and the heights of the bars.
Here, we like to plot 5 bar plots, and the x-coordinates can be generated as follows. This generates an arrayLike data.
Next, we need to generate values that feed an arrayLike data for bar heights. Let’s go ahead and define a function that takes an integer argument, and generates a list of float values, according to the numerical value of the integer, as given below.
According to the above code snippet, the list of values generated is a multiple of 2.
Once the x-coordinates and the heights are ready, now it is time to plot the bars, as follows.
Next, we need to set y-axis limits.
Up to this point, we have created artists to plot a simple static bar plot. We will keep this as a starting point and update the artists on each frame, to create animation. In order to update the artists, we will use a func parameter of the FuncAnimation class.
Now, let’s call the func update function by creating a FuncAnimation object. And, we also need to set parameters for the FuncAnimation object. Most importantly, we need to set the number of frames; the number of frames will dictate the number of containers used to plot the bar graph. For each frame, we will have distinct arrayLike data for bar heights.
Here, frames is set to the variable ‘n’ that holds a numerical value. Every time we call the func parameter, the function iterates through ‘n’.
Now, let’s look at the func function parameter, and see how artists are updated.
As I have mentioned above, every time we call the func function, it iterates through the frames. First of all we will call the function that generates arrayLike data for bar heights.
Here, ‘i’ iteratively takes value of frames: 0, 1, 2,…….,n
Next, we need to iterate through the bar container -barcollection here - , and grab artists for individual bars. Each bar within the bar container is an object of the class 'matplotlib.patches.Rectangle’. We will use enumerate method to iteratively grab the artist and the index. Artist for each bar will be set with a particular height. The height value will be chosen from the arrayLike data, and will be of the same index as that of the bar in the bar container.
Next, we need to update the axes. In order to maintain a continuous motion of the bars and the y-axis, we use the following configuration of the y-axis.
That’s it! we have coded our update function.
Now, we need to save the plot in ‘gif’ format.
We use imagemagick writer for writing the plot to any one of the formats i.e. .mp4, .mkv, .gif
The complete code
Commentaires