I have below code and output, I would like to learn how to make it animated.
def plot_class_distribution(data_path):
class_counts = {}
# Count the number of images in each class
for directory in os.listdir(data_path):
if os.path.isdir(os.path.join(data_path, directory)):
class_counts[directory] = len(os.listdir(os.path.join(data_path, directory)))
# Plot the class distribution as a pie chart
plt.figure(figsize=(8, 6)) # Set the figure size here
plt.pie(class_counts.values(), labels=class_counts.keys(), autopct='%1.1f%%', startangle=140,
textprops={'fontsize': 8})
plt.title('ASL Class Distribution')
plt.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
plt.tight_layout()
plt.show()
plot_class_distribution(data_path)
I have attempted with below code but it failed to output anything.
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import matplotlib.cm as cm
import os
def update(frame):
ax.clear()
ax.pie(frame, labels=class_counts.keys(), autopct='%1.1f%%', startangle=140, textprops={'fontsize': 8})
ax.set_title('ASL Class Distribution')
def plot_class_distribution(data_path):
global class_counts, ax
class_counts = {}
# Count the number of images in each class
for directory in os.listdir(data_path):
if os.path.isdir(os.path.join(data_path, directory)):
class_counts[directory] = len(os.listdir(os.path.join(data_path, directory)))
fig, ax = plt.subplots(figsize=(8, 6))
ani = FuncAnimation(fig, update, frames=class_counts.values(), repeat=False)
plt.show()
plot_class_distribution(data_path)
2