|
1 | 1 | """ anyplot.ai |
2 | 2 | errorbar-basic: Basic Error Bar Plot |
3 | | -Library: matplotlib 3.10.9 | Python 3.14.4 |
4 | | -Quality: 86/100 | Updated: 2026-04-25 |
| 3 | +Library: matplotlib 3.11.0 | Python 3.13.14 |
| 4 | +Quality: 91/100 | Updated: 2026-06-30 |
5 | 5 | """ |
6 | 6 |
|
7 | 7 | import os |
8 | 8 |
|
9 | 9 | import matplotlib.pyplot as plt |
10 | 10 | import numpy as np |
| 11 | +from matplotlib.lines import Line2D |
11 | 12 |
|
12 | 13 |
|
13 | 14 | # Theme tokens |
14 | 15 | THEME = os.getenv("ANYPLOT_THEME", "light") |
15 | 16 | PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17" |
| 17 | +ELEVATED_BG = "#FFFDF6" if THEME == "light" else "#242420" |
16 | 18 | INK = "#1A1A17" if THEME == "light" else "#F0EFE8" |
17 | 19 | INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0" |
18 | | -BRAND = "#009E73" # Okabe-Ito position 1 |
| 20 | +INK_MUTED = "#6B6A63" if THEME == "light" else "#A8A79F" |
| 21 | +BRAND = "#009E73" # Imprint palette position 1 — in-spec machines |
| 22 | +IMPRINT_RED = "#AE3030" # Imprint palette position 5 — out-of-spec machines |
19 | 23 |
|
20 | | -# Data - experimental measurements with associated uncertainties |
21 | | -np.random.seed(42) |
22 | | -categories = ["Control", "Treatment A", "Treatment B", "Treatment C", "Treatment D", "Treatment E"] |
23 | | -x = np.arange(len(categories)) |
24 | | -y = np.array([25.3, 38.7, 42.1, 35.8, 48.2, 31.5]) |
| 24 | +# Data: fill weight calibration across 6 production machines (target: 500 g, USL: 505 g) |
| 25 | +# Machines 1-3 are well-calibrated (symmetric errors); machines 4-6 show process drift (asymmetric) |
| 26 | +machines = ["Machine 1", "Machine 2", "Machine 3", "Machine 4", "Machine 5", "Machine 6"] |
| 27 | +x = np.arange(len(machines)) |
| 28 | +weights = np.array([498.2, 499.8, 502.3, 507.5, 511.8, 518.4]) |
| 29 | +TARGET = 500.0 |
| 30 | +UPPER_SPEC = 505.0 |
25 | 31 |
|
26 | | -# Asymmetric errors: Treatment C and D have notably different lower/upper bounds |
27 | | -asymmetric_lower = np.array([2.1, 3.5, 2.8, 6.5, 4.8, 2.5]) |
28 | | -asymmetric_upper = np.array([2.1, 3.5, 2.8, 2.8, 2.2, 2.5]) |
| 32 | +# Symmetric errors for well-calibrated machines; asymmetric for drifting machines |
| 33 | +errors_lower = np.array([1.5, 1.2, 2.1, 2.8, 3.2, 4.1]) |
| 34 | +errors_upper = np.array([1.5, 1.2, 2.1, 4.5, 6.8, 8.3]) |
| 35 | + |
| 36 | +# Color by compliance: in-spec = brand green, out-of-spec = Imprint red |
| 37 | +point_colors = [BRAND if w <= UPPER_SPEC else IMPRINT_RED for w in weights] |
29 | 38 |
|
30 | 39 | # Plot |
31 | | -fig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG) |
| 40 | +fig, ax = plt.subplots(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG) |
32 | 41 | ax.set_facecolor(PAGE_BG) |
33 | 42 |
|
34 | | -ax.errorbar( |
35 | | - x, |
36 | | - y, |
37 | | - yerr=[asymmetric_lower, asymmetric_upper], |
38 | | - fmt="o", |
39 | | - markersize=15, |
40 | | - color=BRAND, |
41 | | - ecolor=BRAND, |
42 | | - elinewidth=3, |
43 | | - capsize=10, |
44 | | - capthick=3, |
45 | | - markeredgecolor=PAGE_BG, |
46 | | - markeredgewidth=1.2, |
47 | | - alpha=0.95, |
48 | | -) |
| 43 | +# Tolerance band and reference lines drawn first (below data) |
| 44 | +ax.axhspan(TARGET - 5, UPPER_SPEC, color=BRAND, alpha=0.07, zorder=0) |
| 45 | +ax.axhline(TARGET, color=INK_SOFT, linewidth=1.2, linestyle="--", alpha=0.6, zorder=1) |
| 46 | +ax.axhline(UPPER_SPEC, color=IMPRINT_RED, linewidth=1.2, linestyle=":", alpha=0.7, zorder=1) |
| 47 | + |
| 48 | +# One errorbar call per point to support per-machine color coding |
| 49 | +for xi, yi, el, eu, col in zip(x, weights, errors_lower, errors_upper, point_colors, strict=True): |
| 50 | + ax.errorbar( |
| 51 | + xi, |
| 52 | + yi, |
| 53 | + yerr=[[el], [eu]], |
| 54 | + fmt="o", |
| 55 | + markersize=8, |
| 56 | + color=col, |
| 57 | + ecolor=col, |
| 58 | + elinewidth=2.5, |
| 59 | + capsize=7, |
| 60 | + capthick=2.5, |
| 61 | + markeredgecolor=PAGE_BG, |
| 62 | + markeredgewidth=0.8, |
| 63 | + alpha=0.95, |
| 64 | + ) |
49 | 65 |
|
50 | 66 | # Style |
51 | | -ax.set_xlabel("Experimental Group", fontsize=20, color=INK) |
52 | | -ax.set_ylabel("Response Value (units)", fontsize=20, color=INK) |
53 | | -ax.set_title("errorbar-basic · matplotlib · anyplot.ai", fontsize=24, fontweight="medium", color=INK) |
| 67 | +title = "errorbar-basic · python · matplotlib · anyplot.ai" |
| 68 | +ax.set_title(title, fontsize=12, fontweight="medium", color=INK) |
| 69 | +ax.set_xlabel("Production Machine", fontsize=10, color=INK) |
| 70 | +ax.set_ylabel("Fill Weight (g)", fontsize=10, color=INK) |
54 | 71 | ax.set_xticks(x) |
55 | | -ax.set_xticklabels(categories) |
56 | | -ax.tick_params(axis="both", labelsize=16, colors=INK_SOFT) |
| 72 | +ax.set_xticklabels(machines) |
| 73 | +ax.tick_params(axis="both", labelsize=8, colors=INK_SOFT, labelcolor=INK_SOFT) |
57 | 74 | ax.spines["top"].set_visible(False) |
58 | 75 | ax.spines["right"].set_visible(False) |
59 | 76 | for s in ("left", "bottom"): |
60 | 77 | ax.spines[s].set_color(INK_SOFT) |
61 | | -ax.yaxis.grid(True, alpha=0.10, linewidth=0.8, color=INK) |
| 78 | +ax.yaxis.grid(True, alpha=0.12, linewidth=0.8, color=INK) |
62 | 79 | ax.set_axisbelow(True) |
63 | | -ax.set_ylim(0, max(y + asymmetric_upper) * 1.15) |
| 80 | +ax.set_ylim(490, 532) |
| 81 | + |
| 82 | +# Legend — in-spec/out-of-spec groups plus reference lines |
| 83 | +legend_elements = [ |
| 84 | + Line2D( |
| 85 | + [0], |
| 86 | + [0], |
| 87 | + marker="o", |
| 88 | + color="none", |
| 89 | + markerfacecolor=BRAND, |
| 90 | + markeredgecolor=PAGE_BG, |
| 91 | + markersize=8, |
| 92 | + label="In-spec", |
| 93 | + ), |
| 94 | + Line2D( |
| 95 | + [0], |
| 96 | + [0], |
| 97 | + marker="o", |
| 98 | + color="none", |
| 99 | + markerfacecolor=IMPRINT_RED, |
| 100 | + markeredgecolor=PAGE_BG, |
| 101 | + markersize=8, |
| 102 | + label="Out-of-spec", |
| 103 | + ), |
| 104 | + Line2D([0], [0], color=INK_SOFT, linewidth=1.2, linestyle="--", label="Target (500 g)"), |
| 105 | + Line2D([0], [0], color=IMPRINT_RED, linewidth=1.2, linestyle=":", label="Upper spec (505 g)"), |
| 106 | +] |
| 107 | +leg = ax.legend(handles=legend_elements, fontsize=8, loc="upper left") |
| 108 | +leg.get_frame().set_facecolor(ELEVATED_BG) |
| 109 | +leg.get_frame().set_edgecolor(INK_SOFT) |
| 110 | +plt.setp(leg.get_texts(), color=INK_SOFT) |
64 | 111 |
|
| 112 | +# Save |
65 | 113 | plt.tight_layout() |
66 | | -plt.savefig(f"plot-{THEME}.png", dpi=300, bbox_inches="tight", facecolor=PAGE_BG) |
| 114 | +plt.savefig(f"plot-{THEME}.png", dpi=400, facecolor=PAGE_BG) |
0 commit comments