Advertisement
furas

Python - matplotlib - animate pie - (Stackoverflow)

Apr 30th, 2024 (edited)
755
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. # date: 2024.04.30
  2. # [Pie Chart animation in Python doesn't output anything - Stack Overflow](https://stackoverflow.com/questions/78405263/pie-chart-animation-in-python-doesnt-output-anything)
  3.  
  4. import os
  5. import matplotlib.pyplot as plt
  6. from matplotlib.animation import FuncAnimation
  7.  
  8. def get_data(path):
  9.     data = {}
  10.  
  11.     for directory in os.listdir(path):
  12.         full_path = os.path.join(path, directory)
  13.         if os.path.isdir(full_path):
  14.             data[directory] = len(os.listdir(full_path))
  15.    
  16.     return data
  17.  
  18. def update(frame):
  19.     ax.clear()
  20.    
  21.     vals = list(class_counts.values())[:frame]
  22.     labs = list(class_counts.keys())[:frame]
  23.    
  24.     ax.pie(vals, labels=labs, autopct='%1.1f%%', startangle=140, textprops={'fontsize': 8})
  25.  
  26. def plot_class_distribution(class_counts):
  27.     global ax
  28.    
  29.     fig, ax = plt.subplots(figsize=(8, 6))
  30.     ax.set_title('ASL Class Distribution')
  31.  
  32.     # I use `len(class_counts)` as `frames` to slide data in `update`
  33.     ani = FuncAnimation(fig, update, frames=len(class_counts), repeat=False)
  34.     plt.show()
  35.  
  36. # --- main ---
  37.  
  38. path = '/home/furas/Download'
  39.  
  40. class_counts = get_data(path)
  41.  
  42. # skip empty folders
  43. #class_counts = {key:val for key, val in class_counts.items() if val > 0}
  44.  
  45. # show data
  46. for key, val in class_counts.items():
  47.     print(f'{val:3} : {key}')
  48.    
  49. plot_class_distribution(class_counts)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement