- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to properly enable ffmpeg for matplotlib.animation?
To enable ffmpeg for matplotlib.animation, we can take the following steps −
Set the figure size and adjust the padding between and around the subplots.
Set the ffmpeg directory.
Create a new figure or activate an existing figure, using figure() method.
Add an 'ax1' to the figure as part of a subplot arrangement.
Plot the divider based on the pre-existing axes.
Create random data to be plotted, to display the data as an image, i.e., on a 2D regular raster.
Create a colorbar for a ScalarMappable instance, cb.
Set the title as the current frame.
Make a list of colormaps.
Make an animation by repeatedly calling a function, animate. The function creates new random data, then use imshow() method to display the data as an image.
Get an instance of Pipe-based ffmpeg writer.
Save the current animated figure.
Example
import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation from mpl_toolkits.axes_grid1 import make_axes_locatable plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True plt.rcParams['animation.ffmpeg_path'] = 'ffmpeg' fig = plt.figure() ax = fig.add_subplot(111) div = make_axes_locatable(ax) cax = div.append_axes('right', '5%', '5%') data = np.random.rand(5, 5) im = ax.imshow(data) cb = fig.colorbar(im, cax=cax) tx = ax.set_title('Frame 0') cmap = ["copper", 'RdBu_r', 'Oranges', 'cividis', 'hot', 'plasma'] def animate(i): cax.cla() data = np.random.rand(5, 5) im = ax.imshow(data, cmap=cmap[i%len(cmap)]) fig.colorbar(im, cax=cax) tx.set_text('Frame {0}'.format(i)) ani = animation.FuncAnimation(fig, animate, frames=10) FFwriter = animation.FFMpegWriter() ani.save('plot.mp4', writer=FFwriter)
Output
When we execute the code, it will create an mp4 file with the name 'plot.mp4' and save it in the Project Directory.