Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # date: 2024.04.30
- # [Pie Chart animation in Python doesn't output anything - Stack Overflow](https://stackoverflow.com/questions/78405263/pie-chart-animation-in-python-doesnt-output-anything)
- import os
- import matplotlib.pyplot as plt
- from matplotlib.animation import FuncAnimation
- def get_data(path):
- data = {}
- for directory in os.listdir(path):
- full_path = os.path.join(path, directory)
- if os.path.isdir(full_path):
- data[directory] = len(os.listdir(full_path))
- return data
- def update(frame):
- ax.clear()
- vals = list(class_counts.values())[:frame]
- labs = list(class_counts.keys())[:frame]
- ax.pie(vals, labels=labs, autopct='%1.1f%%', startangle=140, textprops={'fontsize': 8})
- def plot_class_distribution(class_counts):
- global ax
- fig, ax = plt.subplots(figsize=(8, 6))
- ax.set_title('ASL Class Distribution')
- # I use `len(class_counts)` as `frames` to slide data in `update`
- ani = FuncAnimation(fig, update, frames=len(class_counts), repeat=False)
- plt.show()
- # --- main ---
- path = '/home/furas/Download'
- class_counts = get_data(path)
- # skip empty folders
- #class_counts = {key:val for key, val in class_counts.items() if val > 0}
- # show data
- for key, val in class_counts.items():
- print(f'{val:3} : {key}')
- plot_class_distribution(class_counts)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement