|
1 | | -""" pyplots.ai |
| 1 | +""" anyplot.ai |
2 | 2 | cat-box-strip: Box Plot with Strip Overlay |
3 | | -Library: matplotlib 3.10.8 | Python 3.13.11 |
4 | | -Quality: 92/100 | Created: 2025-12-30 |
| 3 | +Library: matplotlib 3.10.9 | Python 3.13.13 |
| 4 | +Quality: 91/100 | Updated: 2026-05-13 |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import os |
| 8 | + |
7 | 9 | import matplotlib.pyplot as plt |
8 | 10 | import numpy as np |
9 | 11 |
|
10 | 12 |
|
| 13 | +# Theme tokens |
| 14 | +THEME = os.getenv("ANYPLOT_THEME", "light") |
| 15 | +PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17" |
| 16 | +ELEVATED_BG = "#FFFDF6" if THEME == "light" else "#242420" |
| 17 | +INK = "#1A1A17" if THEME == "light" else "#F0EFE8" |
| 18 | +INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0" |
| 19 | + |
| 20 | +BRAND = "#009E73" # Okabe-Ito position 1 |
| 21 | + |
11 | 22 | # Data - Generate groups with different distributions to showcase features |
12 | 23 | np.random.seed(42) |
13 | 24 |
|
|
31 | 42 | box_data = [data[cat] for cat in categories] |
32 | 43 | positions = np.arange(len(categories)) + 1 |
33 | 44 |
|
34 | | -# Create plot |
35 | | -fig, ax = plt.subplots(figsize=(16, 9)) |
| 45 | +# Plot |
| 46 | +fig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG) |
| 47 | +ax.set_facecolor(PAGE_BG) |
36 | 48 |
|
37 | 49 | # Box plot |
38 | 50 | ax.boxplot( |
|
41 | 53 | tick_labels=categories, |
42 | 54 | widths=0.5, |
43 | 55 | patch_artist=True, |
44 | | - boxprops={"facecolor": "#306998", "alpha": 0.4, "linewidth": 2}, |
45 | | - medianprops={"color": "#FFD43B", "linewidth": 3}, |
46 | | - whiskerprops={"color": "#306998", "linewidth": 2}, |
47 | | - capprops={"color": "#306998", "linewidth": 2}, |
48 | | - flierprops={"marker": "o", "markerfacecolor": "#306998", "markersize": 10, "alpha": 0.7}, |
| 56 | + boxprops={"facecolor": BRAND, "alpha": 0.4, "linewidth": 2, "edgecolor": INK_SOFT}, |
| 57 | + medianprops={"color": "#E69F00", "linewidth": 3}, |
| 58 | + whiskerprops={"color": INK_SOFT, "linewidth": 2}, |
| 59 | + capprops={"color": INK_SOFT, "linewidth": 2}, |
| 60 | + flierprops={ |
| 61 | + "marker": "o", |
| 62 | + "markerfacecolor": BRAND, |
| 63 | + "markersize": 10, |
| 64 | + "alpha": 0.7, |
| 65 | + "markeredgecolor": INK_SOFT, |
| 66 | + "markeredgewidth": 1, |
| 67 | + }, |
49 | 68 | ) |
50 | 69 |
|
51 | 70 | # Strip plot overlay - add jittered points |
52 | 71 | for pos, cat in zip(positions, categories, strict=True): |
53 | 72 | y = data[cat] |
54 | 73 | # Jitter x positions |
55 | 74 | x = np.random.normal(pos, 0.08, len(y)) |
56 | | - ax.scatter(x, y, s=100, alpha=0.6, color="#306998", edgecolor="white", linewidth=1, zorder=3) |
| 75 | + ax.scatter(x, y, s=100, alpha=0.6, color=BRAND, edgecolor=PAGE_BG, linewidth=1, zorder=3) |
| 76 | + |
| 77 | +# Style |
| 78 | +ax.set_xlabel("Treatment Group", fontsize=20, color=INK) |
| 79 | +ax.set_ylabel("Response Value (score)", fontsize=20, color=INK) |
| 80 | +ax.set_title("cat-box-strip · matplotlib · anyplot.ai", fontsize=24, fontweight="medium", color=INK) |
| 81 | +ax.tick_params(axis="both", labelsize=16, colors=INK_SOFT, labelcolor=INK_SOFT) |
| 82 | + |
| 83 | +# Spines |
| 84 | +ax.spines["top"].set_visible(False) |
| 85 | +ax.spines["right"].set_visible(False) |
| 86 | +for s in ("left", "bottom"): |
| 87 | + ax.spines[s].set_color(INK_SOFT) |
57 | 88 |
|
58 | | -# Labels and styling |
59 | | -ax.set_xlabel("Treatment Group", fontsize=20) |
60 | | -ax.set_ylabel("Response Value", fontsize=20) |
61 | | -ax.set_title("cat-box-strip · matplotlib · pyplots.ai", fontsize=24) |
62 | | -ax.tick_params(axis="both", labelsize=16) |
63 | | -ax.grid(True, axis="y", alpha=0.3, linestyle="--") |
| 89 | +# Grid |
| 90 | +ax.yaxis.grid(True, alpha=0.15, linewidth=0.8, color=INK) |
64 | 91 |
|
65 | 92 | # Adjust y-axis to show all data including outliers |
66 | 93 | ax.set_ylim(0, 100) |
67 | 94 |
|
68 | 95 | plt.tight_layout() |
69 | | -plt.savefig("plot.png", dpi=300, bbox_inches="tight") |
| 96 | +plt.savefig(f"plot-{THEME}.png", dpi=300, bbox_inches="tight", facecolor=PAGE_BG) |
0 commit comments