{"spec_id":"histogram-density","library":"letsplot","language":"python","code":"\"\"\" anyplot.ai\nhistogram-density: Density Histogram\nLibrary: letsplot 4.9.0 | Python 3.13.13\nQuality: 78/100 | Updated: 2026-05-11\n\"\"\"\n\nimport numpy as np\nimport pandas as pd\nfrom lets_plot import *\nfrom scipy import stats\n\n\nLetsPlot.setup_html()\n\n# Data - Test scores with normal-like distribution\nnp.random.seed(42)\nscores = np.concatenate(\n    [\n        np.random.normal(72, 8, 300),  # Main group\n        np.random.normal(88, 5, 100),  # High performers\n    ]\n)\n# Clip to realistic test score range\nscores = np.clip(scores, 0, 100)\n\n# Create DataFrame\ndf = pd.DataFrame({\"score\": scores})\n\n# Create theoretical normal distribution for overlay\nx_range = np.linspace(scores.min() - 5, scores.max() + 5, 200)\n# Fit normal distribution to data\nmu, sigma = stats.norm.fit(scores)\ny_pdf = stats.norm.pdf(x_range, mu, sigma)\ndf_pdf = pd.DataFrame({\"x\": x_range, \"y\": y_pdf})\n\n# Create density histogram with KDE overlay\nplot = (\n    ggplot()\n    + geom_histogram(\n        aes(x=\"score\", y=\"..density..\"), data=df, bins=25, fill=\"#306998\", color=\"white\", alpha=0.7, size=0.5\n    )\n    + geom_line(aes(x=\"x\", y=\"y\"), data=df_pdf, color=\"#FFD43B\", size=2.5)\n    + labs(x=\"Test Score\", y=\"Density\", title=\"histogram-density · letsplot · pyplots.ai\")\n    + theme_minimal()\n    + theme(\n        plot_title=element_text(size=24),\n        axis_title=element_text(size=20),\n        axis_text=element_text(size=16),\n        panel_grid_major=element_line(color=\"#CCCCCC\", size=0.3),\n    )\n    + ggsize(1600, 900)\n)\n\n# Save as PNG (scale 3x = 4800 x 2700 px)\nggsave(plot, \"plot.png\", path=\".\", scale=3)\n\n# Save as HTML for interactivity\nggsave(plot, \"plot.html\", path=\".\")\n"}