{"spec_id":"histogram-cumulative","library":"altair","language":"python","code":"\"\"\" anyplot.ai\nhistogram-cumulative: Cumulative Histogram\nLibrary: altair 6.1.0 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-05-11\n\"\"\"\n\nimport os\nimport sys\n\n\n# Handle module import name conflict (script is named altair.py)\nsys.path = [p for p in sys.path if os.path.abspath(p) != os.path.dirname(os.path.abspath(__file__))]\n\nimport altair as alt\nimport numpy as np\nimport pandas as pd\n\n\n# Theme tokens\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\"\nRULE = \"rgba(26,26,23,0.10)\" if THEME == \"light\" else \"rgba(240,239,232,0.10)\"\nBRAND = \"#009E73\"\n\n# Data: Monthly electricity usage (kWh) for households\nnp.random.seed(42)\nusage = np.concatenate([np.random.normal(250, 50, 300), np.random.normal(450, 80, 200)])\n\n# Compute cumulative histogram\nbin_edges = np.linspace(usage.min() - 10, usage.max() + 10, 31)\ncounts, edges = np.histogram(usage, bins=bin_edges)\ncumulative_counts = np.cumsum(counts)\ncumulative_proportion = cumulative_counts / cumulative_counts[-1]\n\n# Create DataFrame for Altair\ndf = pd.DataFrame(\n    {\n        \"Electricity Usage (kWh)\": edges[:-1],\n        \"Cumulative Proportion\": cumulative_proportion,\n        \"Cumulative Count\": cumulative_counts,\n    }\n)\n\n# Create cumulative histogram as step area chart\nchart = (\n    alt.Chart(df)\n    .mark_area(interpolate=\"step-after\", color=BRAND, opacity=0.8, line={\"color\": BRAND, \"strokeWidth\": 3})\n    .encode(\n        x=alt.X(\n            \"Electricity Usage (kWh):Q\",\n            title=\"Electricity Usage (kWh)\",\n            scale=alt.Scale(domain=[bin_edges[0], bin_edges[-1]]),\n        ),\n        y=alt.Y(\"Cumulative Proportion:Q\", title=\"Cumulative Proportion\", scale=alt.Scale(domain=[0, 1])),\n        tooltip=[\n            alt.Tooltip(\"Electricity Usage (kWh):Q\", title=\"Usage (kWh)\", format=\".1f\"),\n            alt.Tooltip(\"Cumulative Proportion:Q\", title=\"Cumulative %\", format=\".1%\"),\n            alt.Tooltip(\"Cumulative Count:Q\", title=\"Count\"),\n        ],\n    )\n    .properties(\n        width=1600,\n        height=900,\n        background=PAGE_BG,\n        title=alt.Title(\"histogram-cumulative · altair · anyplot.ai\", fontSize=28),\n    )\n    .configure_axis(\n        domainColor=INK_SOFT,\n        tickColor=INK_SOFT,\n        gridColor=INK_SOFT,\n        gridOpacity=0.10,\n        labelColor=INK_SOFT,\n        labelFontSize=18,\n        titleColor=INK,\n        titleFontSize=22,\n    )\n    .configure_view(fill=PAGE_BG, stroke=INK_SOFT)\n    .configure_title(color=INK, fontSize=28)\n    .configure_legend(fillColor=ELEVATED_BG, strokeColor=INK_SOFT, labelColor=INK_SOFT, titleColor=INK)\n)\n\n# Save PNG (1600 × 900 at scale 3 = 4800 × 2700)\nchart.save(f\"plot-{THEME}.png\", scale_factor=3.0)\n\n# Save interactive HTML\nchart.save(f\"plot-{THEME}.html\")\n"}