Skip to content

Commit eb64961

Browse files
committed
Enh(analysis): Minor plotting tweaks
1 parent 82b5253 commit eb64961

3 files changed

Lines changed: 14 additions & 21 deletions

File tree

codeclash/analysis/viz/heatmap_win_rates.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from codeclash.constants import LOCAL_LOG_DIR
1414

1515

16-
def main(log_dir: Path, unit: str = "rounds", output_file: Path = ASSETS_DIR / "heatmap_win_rates.png"):
16+
def main(log_dir: Path, unit: str = "rounds", output_file: Path = ASSETS_DIR / "heatmap_win_rates.pdf"):
1717
print(f"Creating win rate heatmap from logs in {log_dir}...")
1818

1919
# Track round-level wins: (model1, model2) -> [wins, total_rounds]
@@ -71,35 +71,37 @@ def main(log_dir: Path, unit: str = "rounds", output_file: Path = ASSETS_DIR / "
7171
# Plot
7272
FONT_BOLD.set_size(18)
7373
_, ax = plt.subplots(figsize=(10, 8))
74-
cmap = mcolors.LinearSegmentedColormap.from_list("br", ["#e74c3c", "#ffffff", "#3498db"])
74+
cmap = mcolors.LinearSegmentedColormap.from_list("rg", ["#f44336", "#ffffff", "#4caf50"]) # Red-Green
75+
7576
masked = np.ma.masked_where(np.isnan(matrix), matrix)
7677
ax.imshow(masked, cmap=cmap, vmin=0, vmax=1)
7778

7879
# Add percentages
7980
for i in range(n):
8081
for j in range(n):
8182
if not np.isnan(matrix[i, j]):
82-
color = "white" if abs(matrix[i, j] - 0.5) > 0.3 else "black"
8383
ax.text(
8484
j,
8585
i,
8686
f"{matrix[i, j]:.0%}",
8787
ha="center",
8888
va="center",
89-
color=color,
89+
color="black",
9090
fontweight="bold",
9191
fontproperties=FONT_BOLD,
92+
fontsize=FONT_BOLD.get_size() + 2,
9293
)
9394

9495
ax.set_xticks(range(n))
9596
ax.set_yticks(range(n))
9697
ax.set_xticklabels(clean_names, rotation=45, ha="right", fontproperties=FONT_BOLD)
9798
ax.set_yticklabels(clean_names, fontproperties=FONT_BOLD)
99+
ax.tick_params(length=0)
98100

99101
# plt.colorbar(im, label="Win Rate")
100102
plt.tight_layout()
101103
if unit == "tournaments":
102-
suffix = output_file.suffix or ".png"
104+
suffix = output_file.suffix or ".pdf"
103105
output_file = output_file.with_name(f"{output_file.stem}_tournaments{suffix}")
104106
plt.savefig(output_file, dpi=300, bbox_inches="tight")
105107
print(f"Heatmap saved to {output_file}")
@@ -110,7 +112,7 @@ def main(log_dir: Path, unit: str = "rounds", output_file: Path = ASSETS_DIR / "
110112
parser.add_argument("-d", "--log_dir", type=Path, default=LOCAL_LOG_DIR, help="Path to logs")
111113
parser.add_argument("-u", "--unit", type=str, default="rounds", help="Unit of analysis: rounds or tournaments")
112114
parser.add_argument(
113-
"-o", "--output_file", type=Path, default=ASSETS_DIR / "heatmap_win_rates.png", help="Output file path"
115+
"-o", "--output_file", type=Path, default=ASSETS_DIR / "heatmap_win_rates.pdf", help="Output file path"
114116
)
115117
args = parser.parse_args()
116118
main(**vars(args))

codeclash/analysis/viz/line_chart_per_round_win_rate.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ def aggregate_win_rates_across_games(profiles):
100100
# Create line chart of win rate progression per player
101101
plt.figure(figsize=(6, 6))
102102
idx = 0
103-
for pid, win_rates in lines.items():
103+
# Sort lines by model name to get consistent legend order
104+
for pid, win_rates in sorted(lines.items()):
104105
plt.plot(
105106
range(1, 16),
106107
win_rates,
@@ -122,28 +123,22 @@ def aggregate_win_rates_across_games(profiles):
122123
fontproperties=FONT_BOLD,
123124
fontsize=14,
124125
)
126+
plt.gca().set_yticks([i / 20 for i in range(2, 21)], minor=True)
125127
plt.ylim(0.1, 1)
126128
FONT_BOLD.set_size(14)
127-
# Get handles and labels, then sort by label alphabetically
128-
handles, labels = plt.gca().get_legend_handles_labels()
129-
sorted_pairs = sorted(zip(handles, labels), key=lambda x: x[1])
130-
sorted_handles, sorted_labels = zip(*sorted_pairs)
131-
132129
plt.legend(
133-
sorted_handles,
134-
sorted_labels,
135130
bbox_to_anchor=(1, 1),
136131
loc="upper right",
137132
ncol=2,
138133
prop=FONT_BOLD,
139134
handletextpad=0.3,
140135
borderpad=0.3,
141-
handlelength=0.5,
136+
handlelength=1.5,
142137
)
143138
plt.grid(True, alpha=0.3)
144139
plt.tight_layout()
145-
plt.savefig(ASSETS_DIR / "line_chart_per_round_win_rate.png", dpi=300, bbox_inches="tight")
146-
print("Win rate progression chart saved to line_chart_per_round_win_rate.png")
140+
plt.savefig(ASSETS_DIR / "line_chart_per_round_win_rate.pdf", dpi=300, bbox_inches="tight")
141+
print("Win rate progression chart saved to line_chart_per_round_win_rate.pdf")
147142

148143
# Print summary statistics
149144
print("\n" + "=" * 50)

codeclash/analysis/viz/recover_after_loss_streak.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ def plot_comeback_probabilities(
201201
"""Plot probability of winning after i consecutive losses."""
202202
fig, ax = plt.subplots(figsize=(6, 6))
203203
label_font = FontProperties(fname=FONT_BOLD.get_file(), size=18)
204-
title_font = FontProperties(fname=FONT_BOLD.get_file(), size=18)
205204
legend_font = FontProperties(fname=FONT_BOLD.get_file(), size=14)
206205

207206
sorted_models = sorted(comeback_prob_df.index)
@@ -239,7 +238,6 @@ def plot_comeback_probabilities(
239238

240239
ax.set_xlabel("Number of Consecutive Rounds Lost", fontproperties=label_font)
241240
ax.set_ylabel("Probability of winning next round", fontproperties=label_font)
242-
ax.set_title("Comeback probability after loss streak", fontproperties=title_font)
243241
ax.legend(loc="upper right", prop=legend_font)
244242
ax.grid(True, alpha=0.3)
245243
ax.yaxis.set_minor_locator(AutoMinorLocator())
@@ -261,7 +259,6 @@ def plot_falldown_probabilities(
261259
"""Plot probability of losing after i consecutive wins."""
262260
fig, ax = plt.subplots(figsize=(6, 6))
263261
label_font = FontProperties(fname=FONT_BOLD.get_file(), size=18)
264-
title_font = FontProperties(fname=FONT_BOLD.get_file(), size=18)
265262
legend_font = FontProperties(fname=FONT_BOLD.get_file(), size=14)
266263

267264
sorted_models = sorted(falldown_prob_df.index)
@@ -299,7 +296,6 @@ def plot_falldown_probabilities(
299296

300297
ax.set_xlabel("Number of Consecutive Rounds Won", fontproperties=label_font)
301298
ax.set_ylabel("Probability of losing next round", fontproperties=label_font)
302-
ax.set_title("Falldown probability after win streak", fontproperties=title_font)
303299
ax.legend(loc="upper right", prop=legend_font)
304300
ax.grid(True, alpha=0.3)
305301
ax.yaxis.set_minor_locator(AutoMinorLocator())

0 commit comments

Comments
 (0)