Skip to content

Commit 57f07fe

Browse files
fix(plotnine): address review feedback for heatmap-cohort-retention
Attempt 2/3 - fixes based on AI review
1 parent 8b1bcdc commit 57f07fe

1 file changed

Lines changed: 15 additions & 14 deletions

File tree

  • plots/heatmap-cohort-retention/implementations

plots/heatmap-cohort-retention/implementations/plotnine.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
""" pyplots.ai
1+
"""pyplots.ai
22
heatmap-cohort-retention: Cohort Retention Heatmap
33
Library: plotnine 0.15.3 | Python 3.14.3
44
Quality: 85/100 | Created: 2026-03-16
@@ -51,7 +51,7 @@
5151
else:
5252
base_decay = 100 * np.exp(-0.25 * period)
5353
noise = np.random.uniform(-3, 3)
54-
trend_bonus = i * 0.4
54+
trend_bonus = i * 1.5
5555
retention = np.clip(base_decay + noise + trend_bonus, 5, 100)
5656
rows.append(
5757
{"cohort": cohort, "period": period, "retention_rate": round(retention, 1), "cohort_size": cohort_sizes[i]}
@@ -72,38 +72,39 @@
7272
# Format retention text
7373
df["label"] = df["retention_rate"].apply(lambda v: f"{v:.0f}%")
7474

75-
# Highlight best-performing cohort at month 3 for storytelling
76-
month3 = df[df["period"] == 3].copy()
77-
best_cohort_idx = month3["retention_rate"].idxmax()
78-
best_cohort = df.loc[best_cohort_idx]
75+
# Compare earliest vs latest cohort at same period for storytelling
76+
compare_period = 4
77+
earliest = df[(df["cohort"] == "Jan 2024") & (df["period"] == compare_period)]["retention_rate"].values[0]
78+
latest = df[(df["cohort"] == "Jun 2024") & (df["period"] == compare_period)]["retention_rate"].values[0]
79+
improvement = latest - earliest
7980

80-
# Multi-stop color palette: deep navy → teal → warm amber for high retention
81-
colors = ["#0d1b2a", "#1b2838", "#1b4965", "#2a6f97", "#62b6cb", "#bee9e8", "#fefae0", "#dda15e"]
81+
# Clean 3-stop color palette: cream → teal → deep navy
82+
colors = ["#0d1b2a", "#2a9d8f", "#fefae0"]
8283

8384
# Plot
8485
plot = (
8586
ggplot(df, aes(x="period", y="cohort_label", fill="retention_rate"))
8687
+ geom_tile(color="#f8f9fa", size=0.6)
87-
+ geom_text(aes(label="label", color="text_color"), size=11, fontweight="bold")
88+
+ geom_text(aes(label="label", color="text_color"), size=13, fontweight="bold")
8889
+ scale_fill_gradientn(colors=colors[::-1], limits=(0, 100), name="Retention %")
8990
+ scale_color_identity()
9091
+ scale_x_continuous(breaks=range(n_cohorts), labels=[f"Month {i}" for i in range(n_cohorts)])
9192
+ scale_y_discrete(expand=(0.05, 0))
9293
+ annotate(
9394
"text",
94-
x=n_cohorts - 1.5,
95+
x=n_cohorts - 2,
9596
y=3,
96-
label=f"Best Month 3:\n{best_cohort['cohort']}\n{best_cohort['retention_rate']:.0f}%",
97-
size=10,
98-
color="#2a6f97",
97+
label=f"Month {compare_period} retention improved\n+{improvement:.0f}pp from Jan→Jun 2024",
98+
size=11,
99+
color="#0d1b2a",
99100
ha="center",
100101
fontweight="bold",
101102
)
102103
+ labs(
103104
x="Months Since Signup",
104105
y="",
105106
title="heatmap-cohort-retention · plotnine · pyplots.ai",
106-
subtitle="Monthly cohort retention — later cohorts show improving retention trends",
107+
subtitle="Monthly cohort retention — newer cohorts retain significantly better over time",
107108
)
108109
+ theme_minimal()
109110
+ theme(

0 commit comments

Comments
 (0)