Matplotlib — quite extensive library that also supports animation charts. The animation tools are centered around the base class matplotlib.animation , which provides the foundation upon which the animation functionality is built. The main interfaces are TimedAnimation and FuncAnimation and of the the most convenient to use is FuncAnimation .
pip install numpy
| tr> |
This is what the output animation looks like:
Now let`s try to understand the code piece by piece:
fig = plt.figure () ax = plt.axes (xlim = (- 50, 50), ylim = (- 50, 50)) line, = ax.plot (, , lw = 2)
Here we first create a shape, that is, a top-level container for all our parcels.
Then we create an axes ax, element that acts as a subplot. The range / limit for the x and y axes is also determined when the axis element is created.
Finally, we create a story element named line . Originally the x and y axis points were defined as empty lists, and the line width (lw) was set to 2.
def init (): line .set_data (, ) return line,
Now we declare the initialization function init . This function is called by the animator to create the first frame.
def animate (i): # t is a parameter t = 0.1 * i # x, y values to be plotted x = t * np.sin (t) y = t * np.cos (t) # appending new points to x, y axes points list xdata.append (x) ydata.append (y) # set / update the x and y axes data line.set_data (xdata, ydata) # return line object return line,
This is the most important function of the above program. The animate () function is called by the animator over and over to create each frame. The number of calls to this function is determined by the number of frames passed as an argument to frames to the animator.
The animate () function takes the index of the i-th frame as an argument.
t = 0.1 * i
Here we cleverly use the index of the current frame as a parameter!
Now, since we have a t parameter , we can easily construct any parametric equation. For example, here we are plotting a spiral using its parametric equation.
line.set_data (xdata, ydata) return line,
Finally, we use the set_data () function strong> to set the x and y data, and then return the plot object, line .
anim = animation.FuncAnimation (fig, animate, init_func = init, frames = 500, interval = 20, blit = True)
Now we create a FuncAnimation object, anim . This takes various arguments, explained below:
pic : The drawing to build.
animate : a function that will be called again for each frame.
init_func : a function used to draw a clean border ... Called once before the first frame.
frames : number of frames. (Note: frames can also be iterables or generators.)
spacing : duration between frames (in milliseconds)
blit strong >: setting blit = True means that only the parts that have changed will be drawn.
anim.save (`animated_coil.mp4`, writer =` ffmpeg`, fps = 30)
Now we save the animator object as a video file using the save () function. You will need a movie maker to save your animated video. In this example, we used the FFMPEG Movie Writer. So writer is set to & # 39; ffmpeg & # 39 ;.
fps stands for frames per second.
This example shows how you can make a rotating curve using simple math!
This is how the result of the above program looks like:
Here we used some simple math to get just rotate this curve.
The same was applied here:
The same was applied here:
In general, animation — it is a great tool for creating amazing things, and with them you can create even more things.
So this is how you can create and save animated plots with Matplotlib.
This article is provided by by Nikhil Kumar . If you like Python.Engineering and would like to contribute, you can also write an article using contrib.python.engineering, or email your article to [email protected] See my article appearing on the Python.Engineering homepage and help other geeks.
Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.
Target knows. Apple Computer knows, too. So do LinkedIn, Netflix, Facebook, Twitter, Expedia, national and local political campaigns, and dozens of other organizations that all generate enormous eco...
Data and storage models are the basis for big data ecosystem stacks. While storage model captures the physical aspects and features for data storage, data model captures the logical representation and...
This book project was first presented to me during my first week in my current role of managing the data mining development at SAS. Writ- ing a book has always been a bucket‐list item, and I was ver...
Google BigQuery: The Definitive Guide PDF download. Data Warehousing, Analytics, and Machine Learning at Scale, 1st Edition, 2019. Work with petabyte-scale datasets while building a collaborative a...