{"spec_id":"histogram-basic","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nhistogram-basic: Basic Histogram\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 91/100 | Updated: 2026-05-28\n\"\"\"\n\nimport os as _os\nimport sys\n\n\n# Prevent the local matplotlib.py in this directory from shadowing the package\n_script_dir = _os.path.realpath(_os.path.dirname(_os.path.abspath(__file__)))\nsys.path[:] = [p for p in sys.path if _os.path.realpath(p if p else \".\") != _script_dir]\ndel _script_dir, _os\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport matplotlib.ticker as ticker\nimport numpy as np\nimport seaborn as sns\n\n\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\n\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\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\n\nIMPRINT_PALETTE = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\"]\nBRAND = IMPRINT_PALETTE[0]\n\n# Data — e-commerce order amounts: two clearly separated spending segments\n# Budget shoppers (~$45) and premium shoppers (~$350) produce a bimodal distribution;\n# mean >> median due to the right tail, giving well-separated reference lines.\nnp.random.seed(42)\nbudget = np.random.lognormal(np.log(45), 0.3, 400)\npremium = np.random.lognormal(np.log(350), 0.35, 350)\nvalues = np.concatenate([budget, premium])\nvalues = values[(values > 5) & (values <= 1200)]\n\nmean_val = np.mean(values)\nmedian_val = np.median(values)\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.15,\n        \"legend.facecolor\": ELEVATED_BG,\n        \"legend.edgecolor\": INK_SOFT,\n    },\n)\n\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400)\n\n# Histogram\nsns.histplot(values, bins=40, color=BRAND, edgecolor=PAGE_BG, linewidth=0.5, alpha=0.88, stat=\"count\", ax=ax)\n\n# KDE overlay via twin axis — seaborn-distinctive, shows both modes as humps\nax2 = ax.twinx()\nsns.kdeplot(values, color=IMPRINT_PALETTE[2], linewidth=1.5, ax=ax2)\nax2.set_ylabel(\"\")\nax2.set_yticks([])\nfor sp in ax2.spines.values():\n    sp.set_visible(False)\n\n# Rugplot — individual observations\nsns.rugplot(values, color=BRAND, alpha=0.05, height=0.025, ax=ax)\n\n# Reference lines for distributional statistics\nmean_line = ax.axvline(mean_val, color=IMPRINT_PALETTE[4], linewidth=1.5, linestyle=\"--\", zorder=5)\nmed_line = ax.axvline(median_val, color=IMPRINT_PALETTE[3], linewidth=1.5, linestyle=\"-.\", zorder=5)\n\ny_top = ax.get_ylim()[1]\n\n# Mode annotations: text placed in the gap between peaks, arrows point to peak tops\nax.annotate(\n    \"Budget shoppers\\n~$45\",\n    xy=(46, y_top * 0.88),\n    xytext=(185, y_top * 0.80),\n    fontsize=7.5,\n    fontstyle=\"italic\",\n    color=INK_SOFT,\n    ha=\"center\",\n    va=\"center\",\n    arrowprops={\"arrowstyle\": \"-|>\", \"color\": INK_SOFT, \"lw\": 0.9},\n)\nax.annotate(\n    \"Premium shoppers\\n~$350\",\n    xy=(355, y_top * 0.42),\n    xytext=(600, y_top * 0.60),\n    fontsize=7.5,\n    fontstyle=\"italic\",\n    color=INK_SOFT,\n    ha=\"center\",\n    va=\"center\",\n    arrowprops={\"arrowstyle\": \"-|>\", \"color\": INK_SOFT, \"lw\": 0.9},\n)\n\n# Legend labels reference lines in the sparse upper-right region\nax.legend(\n    [mean_line, med_line],\n    [f\"Mean  ${mean_val:.0f}\", f\"Median ${median_val:.0f}\"],\n    fontsize=8,\n    loc=\"upper right\",\n    facecolor=ELEVATED_BG,\n    edgecolor=INK_SOFT,\n    framealpha=0.92,\n)\n\n# Typography\nax.set_title(\"histogram-basic · python · seaborn · anyplot.ai\", fontsize=12, fontweight=\"medium\", color=INK, pad=8)\nax.set_xlabel(\"Order Amount (USD)\", fontsize=10, color=INK, labelpad=6)\nax.set_ylabel(\"Number of Orders\", fontsize=10, color=INK, labelpad=6)\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT)\n\n# X-axis dollar formatting\nax.xaxis.set_major_formatter(ticker.FuncFormatter(lambda x, _: f\"${int(x):,}\"))\nax.xaxis.set_major_locator(ticker.MultipleLocator(200))\n\nax.set_xlim(0, 1200)\nax.set_ylim(bottom=0)\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nax.yaxis.grid(True, alpha=0.15, linewidth=0.6, color=INK)\nax.xaxis.grid(False)\n\n# Sample size footnote (lower-right, clear of annotations)\nax.text(\n    0.99,\n    0.04,\n    f\"n = {len(values):,} orders\",\n    transform=ax.transAxes,\n    fontsize=7,\n    color=INK_MUTED,\n    ha=\"right\",\n    va=\"bottom\",\n    fontstyle=\"italic\",\n)\n\nfig.subplots_adjust(left=0.09, right=0.97, top=0.90, bottom=0.13)\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\nplt.close()\n"}