{"spec_id":"histogram-returns-distribution","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nhistogram-returns-distribution: Returns Distribution Histogram\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 88/100 | Updated: 2026-05-20\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport seaborn as sns\nfrom matplotlib.patches import Patch\nfrom scipy import stats\n\n\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\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\"]\n\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\nnp.random.seed(42)\nn_days = 504\ndaily_returns = np.random.normal(loc=0.05, scale=1.5, size=n_days)  # % units\n\nmean_ret = np.mean(daily_returns)\nstd_ret = np.std(daily_returns)\nskewness = stats.skew(daily_returns)\nkurtosis = stats.kurtosis(daily_returns)\n\nlower_tail = mean_ret - 2 * std_ret\nupper_tail = mean_ret + 2 * std_ret\n\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400)\n\nbins = np.linspace(daily_returns.min() - 0.1, daily_returns.max() + 0.1, 32)\n\n# Single histplot on the full dataset so all bars share the same density normalization;\n# recolor tail-region bars afterward (two-call approach normalizes each subset independently)\nsns.histplot(daily_returns, bins=bins, stat=\"density\", color=IMPRINT[0], alpha=0.7, ax=ax)\nfor patch in ax.patches:\n    bin_center = patch.get_x() + patch.get_width() / 2\n    if bin_center < lower_tail or bin_center > upper_tail:\n        patch.set_facecolor(IMPRINT[1])\n        patch.set_alpha(0.85)\n\n# Empirical KDE via seaborn (seaborn-native feature for distribution comparison)\nsns.kdeplot(daily_returns, ax=ax, color=IMPRINT[0], linewidth=1.5, linestyle=\":\", alpha=0.8)\nkde_line = ax.lines[-1]\n\n# Normal distribution curve fitted to the data\nx_range = np.linspace(daily_returns.min() - 0.5, daily_returns.max() + 0.5, 300)\nnormal_pdf = stats.norm.pdf(x_range, mean_ret, std_ret)\nax.plot(x_range, normal_pdf, color=IMPRINT[2], linewidth=2.0)\nnormal_line = ax.lines[-1]\n\n# Vertical dashed lines at ±2σ boundaries\nax.axvline(lower_tail, color=INK_SOFT, linestyle=\"--\", linewidth=1.0, alpha=0.7)\nax.axvline(upper_tail, color=INK_SOFT, linestyle=\"--\", linewidth=1.0, alpha=0.7)\n\n# Statistics text box — header with separator for visual hierarchy\nstats_text = (\n    f\"Statistics\\n\"\n    f\"{'─' * 18}\\n\"\n    f\"Mean: {mean_ret:.3f}%\\n\"\n    f\"Std Dev: {std_ret:.3f}%\\n\"\n    f\"Skewness: {skewness:.3f}\\n\"\n    f\"Kurtosis: {kurtosis:.3f}\"\n)\nax.text(\n    0.975,\n    0.97,\n    stats_text,\n    transform=ax.transAxes,\n    fontsize=7,\n    verticalalignment=\"top\",\n    horizontalalignment=\"right\",\n    bbox={\"boxstyle\": \"round,pad=0.4\", \"facecolor\": ELEVATED_BG, \"edgecolor\": INK_SOFT, \"alpha\": 0.9},\n    color=INK,\n)\n\nax.set_xlabel(\"Daily Returns (%)\", fontsize=10)\nax.set_ylabel(\"Density\", fontsize=10)\nax.set_title(\"histogram-returns-distribution · python · seaborn · anyplot.ai\", fontsize=12, fontweight=\"medium\")\nax.tick_params(axis=\"both\", labelsize=8)\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\n\n# Legend: histogram bars first (primary data), then analytical curves; no frame\ncenter_patch = Patch(facecolor=IMPRINT[0], alpha=0.7, label=\"Returns (±2σ)\")\ntail_patch = Patch(facecolor=IMPRINT[1], alpha=0.7, label=\"Tail regions (>2σ)\")\nkde_line.set_label(\"Empirical KDE\")\nnormal_line.set_label(\"Normal fit\")\nax.legend(handles=[center_patch, tail_patch, kde_line, normal_line], fontsize=8, loc=\"lower left\", frameon=False)\n\nax.yaxis.grid(True, alpha=0.12, linewidth=0.5)\nax.set_axisbelow(True)\n\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\nplt.close()\n"}