MoviePy – Creating Animation Using Matplotlib Last Updated : 14 Jul, 2022 Comments Improve Suggest changes Like Article Like Report In this article we will see how we create animations in MoviePy using matplotlib. MoviePy is a Python module for video editing, which can be used for basic operations on videos and GIF’s. Video is formed by the frames, combination of frames creates a video each frame is an individual image. Matplotlib is a plotting library for the Python programming language and its numerical mathematics extension NumPy. It provides an object-oriented API for embedding plots into applications using general-purpose GUI toolkits like Tkinter, wxPython, Qt, or GTK+. In order to do this we have to do the following 1. Import matplotlib modules 2. Import moviepy modules 3. Create a numpy array 4. Create a subplot using matplotlib 5. Create a Video clip file by calling the make_frame method 6. Inside the make frame method 7. Clear the plot and create a new plot using trigonometry methods according to the frame time 8. Return the plot after converting it into numpy image. Below is the implementation Python3 # importing matplot lib import matplotlib.pyplot as plt import numpy as np # importing movie py libraries from moviepy.editor import VideoClip from moviepy.video.io.bindings import mplfig_to_npimage # numpy array x = np.linspace(-2, 2, 200) # duration of the video duration = 2 # matplot subplot fig, ax = plt.subplots() # method to get frames def make_frame(t): # clear ax.clear() # plotting line ax.plot(x, np.sinc(x**2) + np.sin(x + 2 * np.pi / duration * t), lw = 3) ax.set_ylim(-1.5, 2.5) # returning numpy image return mplfig_to_npimage(fig) # creating animation animation = VideoClip(make_frame, duration = duration) # displaying animation with auto play and looping animation.ipython_display(fps = 20, loop = True, autoplay = True) Output : Moviepy - Building video __temp__.mp4. Moviepy - Writing video __temp__.mp4 Moviepy - Done ! Moviepy - video ready __temp__.mp4 Another example Python3 # importing matplot lib import matplotlib.pyplot as plt import numpy as np # importing movie py libraries from moviepy.editor import VideoClip from moviepy.video.io.bindings import mplfig_to_npimage # numpy array x = np.linspace(-5, 5, 100) # duration of the video duration = 2 # matplot subplot fig, ax = plt.subplots() # method to get frames def make_frame(t): # clear ax.clear() # plotting line ax.plot(x, np.sinc(x**2) + np.cos(x + 10 * np.pi / duration * t), lw = 3) ax.set_ylim(-1.5, 2.5) # returning numpy image return mplfig_to_npimage(fig) # creating animation animation = VideoClip(make_frame, duration = duration) # displaying animation with auto play and looping animation.ipython_display(fps = 20, loop = True, autoplay = True) Output : Moviepy - Building video __temp__.mp4. Moviepy - Writing video __temp__.mp4 Moviepy - Done ! Moviepy - video ready __temp__.mp4 Comment More infoAdvertise with us Next Article MoviePy – Creating Animation Using Matplotlib R rakshitarora Follow Improve Article Tags : Python Python-MoviePy Practice Tags : python Similar Reads Using Matplotlib for Animations Matplotlib library of Python is a plotting tool used to plot graphs of functions or figures. It can also be used as an animation tool too. The plotted graphs when added with animations gives a more powerful visualization and helps the presenter to catch a larger number of audience. Matplotlib can al 5 min read Create an Animated GIF Using Python Matplotlib In this article, we will discuss how to create an animated GIF using Matplotlib in Python. Matplotlib can be used to create mathematics-based animations only. These can include a point moving on the circumference of the circle or a sine or cosine wave which are just like sound waves. In Matplotlib w 3 min read How to save Matplotlib Animation? In this article, we will learn How to save Matplotlib Animation. The animated graphs made with the help of matplotlib can be saved as videos in Python. As we can create captivating animations using the matplotlib library. If you want to learn to create animations, here is a link to the article to cr 2 min read How to animate 3D Graph using Matplotlib? Prerequisites: Matplotlib, NumPy Graphical representations are always easy to understand and are adopted and preferable before any written or verbal communication. With Matplotlib we can draw different types of Graphical data. In this article, we will try to understand, How can we create a beautiful 4 min read Matplotlib.animation.FuncAnimation class in Python Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. matplotlib.animation.FuncAnimation The matplotlib.animation.FuncAnimation class is used 4 min read Like