|
1 | | -""" pyplots.ai |
| 1 | +""" anyplot.ai |
2 | 2 | span-basic: Basic Span Plot (Highlighted Region) |
3 | | -Library: seaborn 0.13.2 | Python 3.13.11 |
4 | | -Quality: 91/100 | Created: 2025-12-23 |
| 3 | +Library: seaborn 0.13.2 | Python 3.13.13 |
| 4 | +Quality: 88/100 | Updated: 2026-04-30 |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import os |
| 8 | + |
7 | 9 | import matplotlib.pyplot as plt |
8 | 10 | import numpy as np |
9 | 11 | import pandas as pd |
10 | 12 | import seaborn as sns |
11 | 13 |
|
12 | 14 |
|
13 | | -# Data - Monthly sales with recession period and target threshold |
| 15 | +# Theme tokens |
| 16 | +THEME = os.getenv("ANYPLOT_THEME", "light") |
| 17 | +PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17" |
| 18 | +ELEVATED_BG = "#FFFDF6" if THEME == "light" else "#242420" |
| 19 | +INK = "#1A1A17" if THEME == "light" else "#F0EFE8" |
| 20 | +INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0" |
| 21 | + |
| 22 | +BRAND = "#009E73" # Okabe-Ito position 1 — always first series |
| 23 | +SPAN_RECESSION = "#D55E00" # Okabe-Ito position 2 |
| 24 | +SPAN_TARGET = "#0072B2" # Okabe-Ito position 3 |
| 25 | + |
| 26 | +sns.set_theme( |
| 27 | + style="ticks", |
| 28 | + rc={ |
| 29 | + "figure.facecolor": PAGE_BG, |
| 30 | + "axes.facecolor": PAGE_BG, |
| 31 | + "axes.edgecolor": INK_SOFT, |
| 32 | + "axes.labelcolor": INK, |
| 33 | + "text.color": INK, |
| 34 | + "xtick.color": INK_SOFT, |
| 35 | + "ytick.color": INK_SOFT, |
| 36 | + "grid.color": INK, |
| 37 | + "grid.alpha": 0.10, |
| 38 | + "legend.facecolor": ELEVATED_BG, |
| 39 | + "legend.edgecolor": INK_SOFT, |
| 40 | + }, |
| 41 | +) |
| 42 | + |
| 43 | +# Data - Monthly sales revenue with recession period and target threshold |
14 | 44 | np.random.seed(42) |
15 | 45 | months = pd.date_range(start="2006-01", periods=60, freq="ME") |
16 | 46 | base_trend = np.linspace(100, 150, 60) |
17 | | -# Dip during recession period (2008-2009) |
18 | 47 | recession_effect = np.where((months >= "2008-01") & (months <= "2009-12"), -30 * np.sin(np.linspace(0, np.pi, 60)), 0) |
19 | 48 | sales = base_trend + recession_effect + np.random.randn(60) * 8 |
20 | | - |
21 | 49 | df = pd.DataFrame({"Month": months, "Sales": sales}) |
22 | 50 |
|
23 | 51 | # Plot |
24 | | -fig, ax = plt.subplots(figsize=(16, 9)) |
| 52 | +fig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG) |
25 | 53 |
|
26 | | -# Vertical span - recession period (2008-2009) |
| 54 | +# Vertical span — recession period (2008–2009) |
27 | 55 | ax.axvspan( |
28 | 56 | pd.Timestamp("2008-01-01"), |
29 | 57 | pd.Timestamp("2009-12-31"), |
30 | 58 | alpha=0.25, |
31 | | - color="#306998", |
32 | | - label="Recession Period (2008-2009)", |
| 59 | + color=SPAN_RECESSION, |
| 60 | + label="Recession (2008–2009)", |
33 | 61 | ) |
34 | 62 |
|
35 | | -# Horizontal span - target sales zone (120-140) |
36 | | -ax.axhspan(120, 140, alpha=0.2, color="#FFD43B", label="Target Zone (120-140)") |
| 63 | +# Horizontal span — target sales zone (120–140) |
| 64 | +ax.axhspan(120, 140, alpha=0.20, color=SPAN_TARGET, label="Target Zone (120–140)") |
37 | 65 |
|
38 | | -# Line plot using seaborn |
39 | | -sns.lineplot(data=df, x="Month", y="Sales", ax=ax, linewidth=3, color="#306998") |
| 66 | +# Line plot |
| 67 | +sns.lineplot(data=df, x="Month", y="Sales", ax=ax, linewidth=3, color=BRAND) |
40 | 68 |
|
41 | | -# Styling |
42 | | -ax.set_title("span-basic · seaborn · pyplots.ai", fontsize=24) |
43 | | -ax.set_xlabel("Month", fontsize=20) |
44 | | -ax.set_ylabel("Sales (thousands $)", fontsize=20) |
45 | | -ax.tick_params(axis="both", labelsize=16) |
| 69 | +# Style |
| 70 | +ax.set_title("span-basic · seaborn · anyplot.ai", fontsize=24, fontweight="medium", color=INK) |
| 71 | +ax.set_xlabel("Month", fontsize=20, color=INK) |
| 72 | +ax.set_ylabel("Sales (thousands $)", fontsize=20, color=INK) |
| 73 | +ax.tick_params(axis="both", labelsize=16, colors=INK_SOFT) |
| 74 | +ax.spines["top"].set_visible(False) |
| 75 | +ax.spines["right"].set_visible(False) |
| 76 | +ax.spines["left"].set_color(INK_SOFT) |
| 77 | +ax.spines["bottom"].set_color(INK_SOFT) |
| 78 | +ax.yaxis.grid(True, alpha=0.10, linewidth=0.8, color=INK) |
46 | 79 | ax.legend(fontsize=16, loc="upper left") |
47 | | -ax.grid(True, alpha=0.3, linestyle="--") |
48 | 80 |
|
49 | 81 | plt.tight_layout() |
50 | | -plt.savefig("plot.png", dpi=300, bbox_inches="tight") |
| 82 | +plt.savefig(f"plot-{THEME}.png", dpi=300, bbox_inches="tight", facecolor=PAGE_BG) |
0 commit comments