Matplotlib অ্যানিমেশন MP4 হিসেবে সেভ করতে FFmpeg লেখক (writer) কনফিগার করতে হয়। plt.rcParams['animation.ffmpeg_path'] সেট করুন এবং animation.FFMpegWriter() ব্যবহার করুন।
- ফিগার সাইজ এবং লেআউট সেট করুন।
plt.rcParams['animation.ffmpeg_path'] = 'ffmpeg'সেট করুন (সিস্টেম PATH-এ ffmpeg থাকতে হবে)।- ফিগার, অ্যাক্সেস, কালারবার, টাইটেল সেটআপ করুন।
FuncAnimationদিয়ে অ্যানিমেশন ফাংশন ডিফাইন করুন।animation.FFMpegWriter()ইনস্ট্যান্স তৈরি করুন।ani.save('plot.mp4', writer=FFwriter)দিয়ে সেভ করুন।
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)
প্রজেক্ট ডিরেক্টরিতে plot.mp4 ফাইল তৈরি হবে যেটিতে ১০ ফ্রেমের অ্যানিমেশন থাকবে, প্রতিটি ফ্রেমে ভিন্ন কালারম্যাপ।
প্রয়োজন: সিস্টেমে FFmpeg ইনস্টল থাকতে হবে (sudo apt install ffmpegবাbrew install ffmpegবা Windows-এ PATH-এ যোগ করুন)।