-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpiechart.py
More file actions
36 lines (29 loc) · 1012 Bytes
/
piechart.py
File metadata and controls
36 lines (29 loc) · 1012 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import pandas as pd
import matplotlib.pyplot as plt
# ---------- Load Data ----------
df = pd.read_csv("montana_label_with_gpe_sorted.csv", usecols=["label"])
df = df.dropna(subset=["label"])
# ---------- Count Label Frequencies ----------
label_counts = df["label"].value_counts()
# ---------- Keep Top 8 Labels, Group Rest as 'Other' ----------
top_labels = label_counts.head(8)
other_count = label_counts[8:].sum()
# Add "Other" if applicable
if other_count > 0:
top_labels["Other"] = other_count
# ---------- Create Pie Chart ----------
plt.figure(figsize=(8, 8))
plt.pie(
top_labels,
labels=top_labels.index,
autopct='%1.1f%%',
startangle=140,
wedgeprops={'edgecolor': 'black'}
)
plt.title("Distribution of Top Labels (Others Grouped)", fontsize=14)
plt.axis('equal') # Keeps pie chart circular
plt.tight_layout()
# ---------- Save and Show ----------
plt.savefig("label_distribution_pie.png", dpi=300)
plt.show()
print("✅ Pie chart saved as 'label_distribution_pie.png'")