|
1 | | -""" pyplots.ai |
| 1 | +""" anyplot.ai |
2 | 2 | bar-stacked: Stacked Bar Chart |
3 | | -Library: matplotlib 3.10.8 | Python 3.13.11 |
4 | | -Quality: 94/100 | Created: 2025-12-26 |
| 3 | +Library: matplotlib 3.10.9 | Python 3.13.13 |
| 4 | +Quality: 90/100 | Updated: 2026-05-09 |
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 |
|
11 | | -# Seed for reproducibility (data is deterministic but seed documents intent) |
12 | | -np.random.seed(42) |
| 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 | +# Okabe-Ito palette (positions 1-4) |
| 21 | +OKABE_ITO = ["#009E73", "#D55E00", "#0072B2", "#CC79A7"] |
13 | 22 |
|
14 | 23 | # Data: Quarterly revenue by product category (in millions USD) |
15 | 24 | categories = ["Q1", "Q2", "Q3", "Q4"] |
16 | 25 | products = ["Software", "Hardware", "Services", "Support"] |
17 | 26 |
|
18 | | -# Revenue data showing realistic business patterns |
19 | | -software = np.array([45, 52, 48, 68]) # Growing, spike in Q4 |
20 | | -hardware = np.array([32, 28, 35, 42]) # Variable with Q4 boost |
21 | | -services = np.array([28, 31, 38, 35]) # Steady growth |
22 | | -support = np.array([15, 18, 20, 22]) # Consistent increase |
23 | | - |
24 | | -# Colors: Python blue primary, then colorblind-safe palette |
25 | | -colors = ["#306998", "#FFD43B", "#4ECDC4", "#FF6B6B"] |
| 27 | +software = np.array([45, 52, 48, 68]) |
| 28 | +hardware = np.array([32, 28, 35, 42]) |
| 29 | +services = np.array([28, 31, 38, 35]) |
| 30 | +support = np.array([15, 18, 20, 22]) |
26 | 31 |
|
27 | | -# Create figure (4800 x 2700 px at 300 dpi = 16 x 9 inches) |
28 | | -fig, ax = plt.subplots(figsize=(16, 9)) |
| 32 | +# Plot |
| 33 | +fig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG) |
| 34 | +ax.set_facecolor(PAGE_BG) |
29 | 35 |
|
30 | | -# Calculate bar positions |
31 | 36 | x = np.arange(len(categories)) |
32 | 37 | bar_width = 0.6 |
33 | 38 |
|
34 | 39 | # Create stacked bars |
35 | 40 | bottom = np.zeros(len(categories)) |
36 | 41 | for i, (product, values) in enumerate(zip(products, [software, hardware, services, support], strict=True)): |
37 | | - ax.bar(x, values, bar_width, label=product, bottom=bottom, color=colors[i], edgecolor="white", linewidth=1.5) |
| 42 | + ax.bar(x, values, bar_width, label=product, bottom=bottom, color=OKABE_ITO[i], edgecolor=PAGE_BG, linewidth=1.5) |
38 | 43 | bottom += values |
39 | 44 |
|
40 | 45 | # Add total labels above each stacked bar |
41 | 46 | totals = software + hardware + services + support |
42 | 47 | for i, total in enumerate(totals): |
43 | | - ax.text(i, total + 3, f"${total}M", ha="center", va="bottom", fontsize=18, fontweight="bold", color="#333333") |
| 48 | + ax.text(i, total + 3, f"${total}M", ha="center", va="bottom", fontsize=18, fontweight="bold", color=INK) |
44 | 49 |
|
45 | | -# Styling |
46 | | -ax.set_xlabel("Quarter", fontsize=20) |
47 | | -ax.set_ylabel("Revenue (Millions USD)", fontsize=20) |
48 | | -ax.set_title("bar-stacked · matplotlib · pyplots.ai", fontsize=24) |
| 50 | +# Style |
| 51 | +ax.set_xlabel("Quarter", fontsize=20, color=INK) |
| 52 | +ax.set_ylabel("Revenue (Millions USD)", fontsize=20, color=INK) |
| 53 | +ax.set_title("bar-stacked · matplotlib · anyplot.ai", fontsize=24, fontweight="medium", color=INK) |
49 | 54 | ax.set_xticks(x) |
50 | 55 | ax.set_xticklabels(categories) |
51 | | -ax.tick_params(axis="both", labelsize=16) |
| 56 | +ax.tick_params(axis="both", labelsize=16, colors=INK_SOFT, labelcolor=INK_SOFT) |
52 | 57 |
|
53 | | -# Legend (outside plot area to avoid overlap with data) |
54 | | -ax.legend(fontsize=16, loc="upper left", bbox_to_anchor=(1.02, 1), framealpha=0.95) |
| 58 | +# Legend |
| 59 | +leg = ax.legend(fontsize=16, loc="upper left", bbox_to_anchor=(1.02, 1)) |
| 60 | +if leg: |
| 61 | + leg.get_frame().set_facecolor(ELEVATED_BG) |
| 62 | + leg.get_frame().set_edgecolor(INK_SOFT) |
| 63 | + plt.setp(leg.get_texts(), color=INK_SOFT) |
55 | 64 |
|
56 | | -# Grid (subtle, horizontal only for bar charts) |
57 | | -ax.yaxis.grid(True, alpha=0.3, linestyle="--") |
| 65 | +# Grid (subtle, y-axis only) |
| 66 | +ax.yaxis.grid(True, alpha=0.15, linewidth=0.8, color=INK) |
58 | 67 | ax.set_axisbelow(True) |
59 | 68 |
|
60 | | -# Clean up spines |
| 69 | +# Spines |
61 | 70 | ax.spines["top"].set_visible(False) |
62 | 71 | ax.spines["right"].set_visible(False) |
| 72 | +for s in ("left", "bottom"): |
| 73 | + ax.spines[s].set_color(INK_SOFT) |
63 | 74 |
|
64 | | -# Set y-axis to start at 0 and add headroom for labels |
| 75 | +# Y-axis limits with headroom for labels |
65 | 76 | ax.set_ylim(0, max(totals) + 20) |
66 | 77 |
|
67 | 78 | plt.tight_layout() |
68 | | -plt.savefig("plot.png", dpi=300, bbox_inches="tight") |
| 79 | +plt.savefig(f"plot-{THEME}.png", dpi=300, bbox_inches="tight", facecolor=PAGE_BG) |
0 commit comments