-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot_utils.py
More file actions
77 lines (57 loc) · 1.94 KB
/
Copy pathplot_utils.py
File metadata and controls
77 lines (57 loc) · 1.94 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import matplotlib.pyplot as plt
import matplotlib as mpl
import os
import hashlib
import colorsys
def get_unique_color(label):
# Ensure we're dealing with a string
label_str = str(label)
# Compute SHA-1 hash of the label
label_hash = hashlib.sha1(label_str.encode('utf-8')).hexdigest()
hash_int = int(label_hash, 16)
# Use part of that integer to define our hue in [0,1]
# (modulo by 360 first, then divide by 360)
hue = (hash_int % 360) / 360.0
# Fix saturation and lightness for a visually distinct color
saturation = 0.7
lightness = 0.5
# Convert from HLS to RGB in [0,1]
r, g, b = colorsys.hls_to_rgb(hue, lightness, saturation)
# Scale to [0,255] and format as hex
r_int, g_int, b_int = int(r*255), int(g*255), int(b*255)
color_hex = f"#{r_int:02x}{g_int:02x}{b_int:02x}"
return color_hex
def plot_value_counts(
value_counts,
legend_title="Corruption Type",
y_label="Proportion",
figsize=(6, 3),
palette=None,
save_filename: str = "proportions"
):
fig, ax = plt.subplots(figsize=figsize)
# Dynamically compute colors for each label
labels = value_counts.index
colors = [get_unique_color(lbl) for lbl in labels]
# Plot the bar chart with computed colors
value_counts.plot(kind="bar", color=colors, ax=ax)
# Build legend handles for each label in the same order
handles = [
mpl.patches.Patch(color=c, label=lbl)
for c, lbl in zip(colors, labels)
]
# Place the legend to the right
ax.legend(
handles=handles,
title=legend_title,
bbox_to_anchor=(1.05, 1),
loc="upper left",
)
# Remove x-axis label and tick labels
ax.set(xlabel="", ylabel=y_label)
ax.set_xticklabels([])
plt.tight_layout()
# Save the figure
os.makedirs("la_output/plots", exist_ok=True)
plt.savefig(f"la_output/plots/{save_filename}.png", bbox_inches='tight', dpi=300)
plt.show()