Matplotlib-এ অ্যানিমেশন ব্যবহার করে প্লটের টাইটেল বা টেক্সট ডাইনামিক্যালি আপডেট করতে FuncAnimation ব্যবহার করা হয়।
ধাপসমূহ
- ফিগার সাইজ ও প্যাডিং সেট করুন
figure()দিয়ে ফিগার তৈরি করুন- NumPy দিয়ে x ও y ডেটা পয়েন্ট তৈরি করুন
gca()দিয়ে কারেন্ট অ্যাক্স নিনtext()দিয়ে অ্যাক্সে টেক্সট যোগ করুন (বক্স স্টাইলসহ)animate()ফাংশন লিখুন যা ফ্রেম অনুযায়ী টেক্সট আপডেট করবেFuncAnimationদিয়ে অ্যানিমেশন তৈরি করুনshow()মেথড দিয়ে ফিগার প্রদর্শন করুন
উদাহরণ
import numpy as np
from matplotlib import pyplot as plt, animation
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure()
x = np.linspace(-10, 10, 1000)
y = np.sin(x)
plt.plot(x, y)
ax = plt.gca()
ax.text(0.5, 1.100, "y=sin(x)",
bbox={'facecolor': 'red', 'alpha': 0.5, 'pad': 5},
transform=ax.transAxes, ha="center")
def animate(frame):
ax.text(0.5, 1.100, "y=sin(x), frame: %d" % frame,
bbox={'facecolor': 'red', 'alpha': 0.5, 'pad': 5},
transform=ax.transAxes, ha="center")
ani = animation.FuncAnimation(fig, animate, interval=100, frames=10, repeat=True)
plt.show()
আউটপুট
