Skip to content

Commit f7e8785

Browse files
Merge branch 'main' into implementation/ecdf-basic/bokeh
2 parents 3612e90 + ac35e4c commit f7e8785

10 files changed

Lines changed: 798 additions & 532 deletions

File tree

Lines changed: 99 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
""" anyplot.ai
22
ecdf-basic: Basic ECDF Plot
3-
Library: altair 6.1.0 | Python 3.14.4
4-
Quality: 86/100 | Updated: 2026-04-24
3+
Library: altair 6.2.2 | Python 3.13.14
4+
Quality: 90/100 | Updated: 2026-06-25
55
"""
66

77
import os
8+
import sys
9+
10+
11+
# The file is named altair.py; remove its own directory from sys.path so
12+
# `import altair` resolves to the library, not this script.
13+
_HERE = os.path.dirname(os.path.abspath(__file__))
14+
sys.path = [p for p in sys.path if not p or os.path.abspath(p) != _HERE]
15+
os.chdir(_HERE) # saves (plot-*.png, plot-*.html) land in the implementations dir
816

917
import altair as alt
1018
import numpy as np
1119
import pandas as pd
20+
from PIL import Image
1221

1322

1423
# Theme tokens
@@ -17,56 +26,126 @@
1726
ELEVATED_BG = "#FFFDF6" if THEME == "light" else "#242420"
1827
INK = "#1A1A17" if THEME == "light" else "#F0EFE8"
1928
INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0"
20-
BRAND = "#009E73"
29+
INK_MUTED = "#6B6A63" if THEME == "light" else "#A8A79F"
30+
BRAND = "#009E73" # Imprint palette position 1 — always first series
2131

2232
# Data: API response latency from a production web service
2333
np.random.seed(42)
2434
response_times_ms = np.random.normal(loc=120, scale=35, size=250)
2535
response_times_ms = np.clip(response_times_ms, 20, None)
2636

27-
sorted_latency = np.sort(response_times_ms)
28-
cumulative_proportion = np.arange(1, len(sorted_latency) + 1) / len(sorted_latency)
37+
# Raw data frame — ECDF computed declaratively via Vega-Lite window transform
38+
df = pd.DataFrame({"latency_ms": response_times_ms})
2939

30-
df = pd.DataFrame({"latency_ms": sorted_latency, "cumulative": cumulative_proportion})
40+
# Reference values at quartiles for focal emphasis and text annotations
41+
p25_ms = float(np.percentile(response_times_ms, 25))
42+
p50_ms = float(np.median(response_times_ms))
43+
p75_ms = float(np.percentile(response_times_ms, 75))
44+
ref_df = pd.DataFrame(
45+
{
46+
"latency_ms": [p25_ms, p50_ms, p75_ms],
47+
"cumulative": [0.25, 0.50, 0.75],
48+
"label": [f"~{p25_ms:.0f}ms", f"~{p50_ms:.0f}ms", f"~{p75_ms:.0f}ms"],
49+
}
50+
)
3151

32-
# Chart
33-
chart = (
52+
# Title
53+
title_str = "ecdf-basic · python · altair · anyplot.ai"
54+
55+
# ECDF step function — cume_dist() window transform computes the ECDF declaratively
56+
# in Vega-Lite without numpy preprocessing; step-after gives the correct step shape
57+
ecdf_line = (
3458
alt.Chart(df)
59+
.transform_window(ecdf="cume_dist()", sort=[alt.SortField("latency_ms")])
3560
.mark_line(interpolate="step-after", strokeWidth=3.5, color=BRAND)
3661
.encode(
3762
x=alt.X("latency_ms:Q", title="API Response Time (ms)", scale=alt.Scale(nice=True)),
3863
y=alt.Y(
39-
"cumulative:Q",
64+
"ecdf:Q",
4065
title="Cumulative Proportion",
4166
scale=alt.Scale(domain=[0, 1]),
4267
axis=alt.Axis(format=".0%", tickCount=11),
4368
),
4469
tooltip=[
4570
alt.Tooltip("latency_ms:Q", title="Latency (ms)", format=".1f"),
46-
alt.Tooltip("cumulative:Q", title="Proportion", format=".3f"),
71+
alt.Tooltip("ecdf:Q", title="Proportion", format=".3f"),
4772
],
4873
)
74+
)
75+
76+
# Dashed reference lines spanning the full axes at Q1, median, Q3
77+
h_rules = (
78+
alt.Chart(ref_df)
79+
.mark_rule(strokeDash=[5, 4], strokeWidth=1.5, color=INK_MUTED, opacity=0.75)
80+
.encode(y="cumulative:Q")
81+
)
82+
83+
v_rules = (
84+
alt.Chart(ref_df)
85+
.mark_rule(strokeDash=[5, 4], strokeWidth=1.5, color=INK_MUTED, opacity=0.75)
86+
.encode(x="latency_ms:Q")
87+
)
88+
89+
# Focal markers at quartile intersections on the ECDF
90+
focal_pts = (
91+
alt.Chart(ref_df)
92+
.mark_point(size=120, filled=True, color=BRAND, opacity=1.0)
93+
.encode(
94+
x="latency_ms:Q",
95+
y="cumulative:Q",
96+
tooltip=[
97+
alt.Tooltip("latency_ms:Q", title="Latency (ms)", format=".1f"),
98+
alt.Tooltip("cumulative:Q", title="Quartile", format=".0%"),
99+
],
100+
)
101+
)
102+
103+
# Text annotations at focal points for at-a-glance percentile reading without hover
104+
focal_labels = (
105+
alt.Chart(ref_df)
106+
.mark_text(align="left", dx=8, dy=-5, fontSize=9, color=INK_SOFT, fontWeight="bold")
107+
.encode(x="latency_ms:Q", y="cumulative:Q", text="label:N")
108+
)
109+
110+
# Compose layers and configure theme-adaptive chrome
111+
chart = (
112+
alt.layer(ecdf_line, h_rules, v_rules, focal_pts, focal_labels)
113+
.interactive()
49114
.properties(
50-
width=1600,
51-
height=900,
115+
width=620,
116+
height=320,
52117
background=PAGE_BG,
53-
title=alt.Title("ecdf-basic · altair · anyplot.ai", fontSize=28, color=INK),
118+
padding={"left": 0, "right": 0, "top": 0, "bottom": 0},
119+
title=alt.Title(title_str, fontSize=16, color=INK),
54120
)
55-
.interactive()
56-
.configure_view(fill=PAGE_BG, strokeWidth=0)
121+
.configure_view(fill=PAGE_BG, stroke=INK_SOFT, strokeWidth=0, continuousWidth=620, continuousHeight=320)
57122
.configure_axis(
58123
domainColor=INK_SOFT,
59124
tickColor=INK_SOFT,
60-
gridColor=INK,
61-
gridOpacity=0.10,
62125
labelColor=INK_SOFT,
63126
titleColor=INK,
64-
labelFontSize=18,
65-
titleFontSize=22,
127+
labelFontSize=10,
128+
titleFontSize=12,
66129
)
130+
.configure_axisX(grid=False)
131+
.configure_axisY(gridColor=INK, gridOpacity=0.13)
67132
.configure_title(color=INK)
68133
)
69134

70135
# Save
71-
chart.save(f"plot-{THEME}.png", scale_factor=3.0)
136+
chart.save(f"plot-{THEME}.png", scale_factor=4.0)
72137
chart.save(f"plot-{THEME}.html")
138+
139+
# Canvas: pad to exactly 3200×1800 with PAGE_BG (vl-convert inner-view padding lands short)
140+
TW, TH = 3200, 1800
141+
_img = Image.open(f"plot-{THEME}.png").convert("RGB")
142+
_w, _h = _img.size
143+
if _w > TW or _h > TH:
144+
raise SystemExit(
145+
f"altair vl-convert produced {_w}×{_h}, exceeds target {TW}×{TH}. "
146+
f"Shrink chart .properties(width=, height=) values and re-render."
147+
)
148+
if _w < TW or _h < TH:
149+
_canvas = Image.new("RGB", (TW, TH), PAGE_BG)
150+
_canvas.paste(_img, ((TW - _w) // 2, (TH - _h) // 2))
151+
_canvas.save(f"plot-{THEME}.png")
Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
""" anyplot.ai
22
ecdf-basic: Basic ECDF Plot
3-
Library: matplotlib 3.10.9 | Python 3.14.4
4-
Quality: 89/100 | Updated: 2026-04-24
3+
Library: matplotlib 3.11.0 | Python 3.13.14
4+
Quality: 91/100 | Updated: 2026-06-25
55
"""
66

77
import os
@@ -10,52 +10,65 @@
1010
import numpy as np
1111

1212

13-
# Theme
1413
THEME = os.getenv("ANYPLOT_THEME", "light")
1514
PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17"
1615
ELEVATED_BG = "#FFFDF6" if THEME == "light" else "#242420"
1716
INK = "#1A1A17" if THEME == "light" else "#F0EFE8"
1817
INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0"
1918
INK_MUTED = "#6B6A63" if THEME == "light" else "#A8A79F"
20-
BRAND = "#009E73"
19+
BRAND = "#009E73" # Imprint palette position 1
2120

22-
# Data: API response times (ms) — log-normal distribution with a long tail
23-
np.random.seed(42)
24-
response_times_ms = np.random.lognormal(mean=4.6, sigma=0.55, size=250)
21+
# Data: IoT sensor temperature readings (°C) — bimodal mix of offices and server rooms
22+
np.random.seed(7)
23+
office = np.random.normal(loc=22.0, scale=1.8, size=160)
24+
servers = np.random.normal(loc=18.5, scale=0.9, size=90)
25+
temps = np.concatenate([office, servers])
26+
t_min = temps.min()
2527

26-
# Plot
27-
fig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)
28+
# Canvas — 3200 × 1800 px landscape (figsize × dpi, no bbox_inches='tight')
29+
fig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)
2830
ax.set_facecolor(PAGE_BG)
2931

30-
ax.ecdf(response_times_ms, color=BRAND, linewidth=3.5)
32+
# Subtle fill under the ECDF curve for visual weight
33+
sorted_temps = np.sort(temps)
34+
ecdf_y = np.arange(1, len(sorted_temps) + 1) / len(sorted_temps)
35+
ax.fill_between(sorted_temps, 0, ecdf_y, step="post", alpha=0.10, color=BRAND)
3136

32-
# Percentile reference lines (p50, p95, p99) — common SRE reading
33-
percentiles = [50, 95, 99]
34-
percentile_values = np.percentile(response_times_ms, percentiles)
35-
for p, v in zip(percentiles, percentile_values, strict=True):
36-
ax.axhline(y=p / 100, color=INK_SOFT, linestyle=":", linewidth=1.5, alpha=0.6)
37-
ax.axvline(x=v, ymax=p / 100, color=INK_SOFT, linestyle=":", linewidth=1.5, alpha=0.6)
37+
# ECDF step function (drawn on top of fill)
38+
ax.ecdf(temps, color=BRAND, linewidth=2.5)
39+
40+
# Bimodal mode markers — highlight the two sensor populations
41+
for mode_temp, label in [(18.5, "Server\nrooms"), (22.0, "Offices")]:
42+
ax.axvline(mode_temp, color=INK_MUTED, linestyle="--", linewidth=0.8, alpha=0.45)
43+
ax.text(mode_temp + 0.2, 0.05, label, fontsize=8, color=INK_MUTED, va="bottom", ha="left")
44+
45+
# Quartile reference guides (Q1, Q2, Q3)
46+
quartiles = [25, 50, 75]
47+
q_values = np.percentile(temps, quartiles)
48+
for q, v in zip(quartiles, q_values, strict=True):
49+
yf = q / 100
50+
ax.plot([t_min - 0.3, v], [yf, yf], color=INK_MUTED, linestyle=":", linewidth=0.8, alpha=0.6)
51+
ax.plot([v, v], [0, yf], color=INK_MUTED, linestyle=":", linewidth=0.8, alpha=0.6)
3852
ax.annotate(
39-
f"p{p} {v:.0f} ms",
40-
xy=(v, p / 100),
41-
xytext=(10, -18 if p == 99 else 8),
42-
textcoords="offset points",
43-
fontsize=14,
44-
color=INK_SOFT,
53+
f"Q{q // 25} {v:.1f}°C", xy=(v, yf), xytext=(5, 5), textcoords="offset points", fontsize=9, color=INK_MUTED
4554
)
4655

47-
# Style
48-
ax.set_xlabel("Response Time (ms)", fontsize=20, color=INK)
49-
ax.set_ylabel("Cumulative Proportion of Requests", fontsize=20, color=INK)
50-
ax.set_title("API Response Times · ecdf-basic · matplotlib · anyplot.ai", fontsize=24, fontweight="medium", color=INK)
51-
ax.tick_params(axis="both", labelsize=16, colors=INK_SOFT)
56+
# Chrome
57+
title = "Sensor Temperatures · ecdf-basic · python · matplotlib · anyplot.ai"
58+
title_fs = max(8, round(12 * 67 / len(title))) if len(title) > 67 else 12
59+
ax.set_title(title, fontsize=title_fs, fontweight="medium", color=INK)
60+
ax.set_xlabel("Temperature (°C)", fontsize=10, color=INK)
61+
ax.set_ylabel("Cumulative Proportion", fontsize=10, color=INK)
62+
ax.tick_params(axis="both", labelsize=8, colors=INK_SOFT, labelcolor=INK_SOFT)
5263
ax.set_ylim(0, 1.02)
53-
ax.set_xlim(left=0)
64+
ax.set_xlim(left=t_min - 0.5)
65+
5466
ax.spines["top"].set_visible(False)
5567
ax.spines["right"].set_visible(False)
5668
for s in ("left", "bottom"):
5769
ax.spines[s].set_color(INK_SOFT)
58-
ax.grid(True, alpha=0.10, linewidth=0.8, color=INK)
5970

60-
plt.tight_layout()
61-
plt.savefig(f"plot-{THEME}.png", dpi=300, bbox_inches="tight", facecolor=PAGE_BG)
71+
ax.yaxis.grid(True, alpha=0.15, linewidth=0.8, color=INK)
72+
73+
fig.subplots_adjust(left=0.09, right=0.97, top=0.92, bottom=0.12)
74+
plt.savefig(f"plot-{THEME}.png", dpi=400, facecolor=PAGE_BG)

0 commit comments

Comments
 (0)