{"spec_id":"histogram-stacked","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nhistogram-stacked: Stacked Histogram\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 89/100 | Updated: 2026-05-12\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\n\n\n# Get theme from environment\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nELEVATED_BG = \"#FFFDF6\" if THEME == \"light\" else \"#242420\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\n\n# Set theme-aware colors\nsns.set_theme(\n    style=\"ticks\",\n    rc={\n        \"figure.facecolor\": PAGE_BG,\n        \"axes.facecolor\": PAGE_BG,\n        \"axes.edgecolor\": INK_SOFT,\n        \"axes.labelcolor\": INK,\n        \"text.color\": INK,\n        \"xtick.color\": INK_SOFT,\n        \"ytick.color\": INK_SOFT,\n        \"grid.color\": INK,\n        \"grid.alpha\": 0.10,\n        \"legend.facecolor\": ELEVATED_BG,\n        \"legend.edgecolor\": INK_SOFT,\n    },\n)\n\n# Data - Response times (ms) from three different server regions\nnp.random.seed(42)\n\n# Generate response times for three server regions with different characteristics\nregion_a = np.random.normal(loc=45, scale=12, size=150)  # US East - faster\nregion_b = np.random.normal(loc=60, scale=15, size=180)  # Europe - medium\nregion_c = np.random.normal(loc=75, scale=18, size=120)  # Asia Pacific - slower\n\n# Combine into DataFrame\ndf = pd.DataFrame(\n    {\n        \"Response Time (ms)\": np.concatenate([region_a, region_b, region_c]),\n        \"Region\": ([\"US East\"] * len(region_a) + [\"Europe\"] * len(region_b) + [\"Asia Pacific\"] * len(region_c)),\n    }\n)\n\n# Plot\nfig, ax = plt.subplots(figsize=(16, 9))\n\n# Okabe-Ito palette for stacked histogram\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\"]\n\n# Stacked histogram using histplot with multiple='stack'\nsns.histplot(\n    data=df,\n    x=\"Response Time (ms)\",\n    hue=\"Region\",\n    hue_order=[\"US East\", \"Europe\", \"Asia Pacific\"],\n    multiple=\"stack\",\n    bins=20,\n    palette=IMPRINT,\n    edgecolor=\"white\",\n    linewidth=0.8,\n    alpha=0.9,\n    ax=ax,\n)\n\n# Styling\nax.set_xlabel(\"Response Time (ms)\", fontsize=20)\nax.set_ylabel(\"Frequency\", fontsize=20)\nax.set_title(\"histogram-stacked · seaborn · anyplot.ai\", fontsize=24)\nax.tick_params(axis=\"both\", labelsize=16)\nax.grid(True, alpha=0.1, linestyle=\"-\", axis=\"y\")\n\n# Adjust legend styling\nlegend = ax.get_legend()\nlegend.set_title(\"Server Region\")\nlegend.get_title().set_fontsize(16)\nfor text in legend.get_texts():\n    text.set_fontsize(14)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}