{"spec_id":"histogram-density","library":"pygal","language":"python","code":"\"\"\" anyplot.ai\nhistogram-density: Density Histogram\nLibrary: pygal 3.1.0 | Python 3.13.13\nQuality: 82/100 | Updated: 2026-05-11\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport pygal\nfrom pygal.style import Style\n\n\n# Theme configuration\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\n\n# Color palette - Okabe-Ito + theme-adaptive chrome\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\n\nIMPRINT = (\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\")\n\n# Generate sample data - test scores with realistic distribution\nnp.random.seed(42)\nscores_group1 = np.random.normal(loc=65, scale=10, size=150)\nscores_group2 = np.random.normal(loc=82, scale=8, size=100)\nscores = np.concatenate([scores_group1, scores_group2])\nscores = np.clip(scores, 0, 100)\n\n# Calculate density histogram\nn_bins = 25\ncounts, bin_edges = np.histogram(scores, bins=n_bins, density=True)\nbin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2\n\n# Create custom style matching default-style-guide.md\ncustom_style = Style(\n    background=PAGE_BG,\n    plot_background=PAGE_BG,\n    foreground=INK,\n    foreground_strong=INK,\n    foreground_subtle=INK_MUTED,\n    colors=IMPRINT,\n    title_font_size=28,\n    label_font_size=22,\n    major_label_font_size=18,\n    legend_font_size=16,\n    value_font_size=14,\n    stroke_width=3,\n)\n\n# Create histogram chart\nchart = pygal.Bar(\n    width=4800,\n    height=2700,\n    style=custom_style,\n    title=\"histogram-density · pygal · anyplot.ai\",\n    x_title=\"Test Score\",\n    y_title=\"Density (Probability per Unit)\",\n    show_legend=False,\n    show_x_guides=False,\n    show_y_guides=True,\n    x_label_rotation=0,\n    margin=120,\n    spacing=1,\n    print_values=False,\n)\n\n# Format x-axis labels (show every 5th bin for clarity)\nchart.x_labels = [f\"{int(bc)}\" if i % 5 == 0 else \"\" for i, bc in enumerate(bin_centers)]\n\n# Add density histogram data\nchart.add(\"\", [float(c) for c in counts])\n\n# Save outputs with theme-suffixed filenames\nchart.render_to_png(f\"plot-{THEME}.png\")\nwith open(f\"plot-{THEME}.html\", \"wb\") as f:\n    f.write(chart.render())\n"}