|
| 1 | +""" pyplots.ai |
| 2 | +histogram-density: Density Histogram |
| 3 | +Library: highcharts unknown | Python 3.13.11 |
| 4 | +Quality: 92/100 | Created: 2025-12-29 |
| 5 | +""" |
| 6 | + |
| 7 | +import tempfile |
| 8 | +import time |
| 9 | +import urllib.request |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +import numpy as np |
| 13 | +from highcharts_core.chart import Chart |
| 14 | +from highcharts_core.options import HighchartsOptions |
| 15 | +from highcharts_core.options.series.area import AreaSplineSeries |
| 16 | +from highcharts_core.options.series.bar import ColumnSeries |
| 17 | +from selenium import webdriver |
| 18 | +from selenium.webdriver.chrome.options import Options |
| 19 | + |
| 20 | + |
| 21 | +# Data - Generate test score data with realistic distribution |
| 22 | +np.random.seed(42) |
| 23 | +values = np.random.normal(loc=75, scale=12, size=500) # Test scores centered around 75 |
| 24 | + |
| 25 | +# Calculate density histogram |
| 26 | +n_bins = 25 |
| 27 | +counts, bin_edges = np.histogram(values, bins=n_bins, density=True) |
| 28 | +bin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2 |
| 29 | +bin_width = bin_edges[1] - bin_edges[0] |
| 30 | + |
| 31 | +# Generate theoretical normal PDF for overlay (computed manually) |
| 32 | +x_pdf = np.linspace(values.min() - 5, values.max() + 5, 200) |
| 33 | +mean_val = np.mean(values) |
| 34 | +std_val = np.std(values) |
| 35 | +y_pdf = (1 / (std_val * np.sqrt(2 * np.pi))) * np.exp(-0.5 * ((x_pdf - mean_val) / std_val) ** 2) |
| 36 | + |
| 37 | +# Create chart |
| 38 | +chart = Chart(container="container") |
| 39 | +chart.options = HighchartsOptions() |
| 40 | + |
| 41 | +# Chart configuration |
| 42 | +chart.options.chart = { |
| 43 | + "type": "column", |
| 44 | + "width": 4800, |
| 45 | + "height": 2700, |
| 46 | + "backgroundColor": "#ffffff", |
| 47 | + "marginBottom": 220, |
| 48 | + "marginLeft": 220, |
| 49 | + "marginRight": 100, |
| 50 | + "marginTop": 150, |
| 51 | + "spacingBottom": 30, |
| 52 | +} |
| 53 | + |
| 54 | +# Title |
| 55 | +chart.options.title = { |
| 56 | + "text": "histogram-density · highcharts · pyplots.ai", |
| 57 | + "style": {"fontSize": "48px", "fontWeight": "bold"}, |
| 58 | +} |
| 59 | + |
| 60 | +# Subtitle for context |
| 61 | +chart.options.subtitle = { |
| 62 | + "text": "Test Score Distribution with Normal PDF Overlay", |
| 63 | + "style": {"fontSize": "32px", "color": "#666666"}, |
| 64 | +} |
| 65 | + |
| 66 | +# X-axis |
| 67 | +chart.options.x_axis = { |
| 68 | + "title": {"text": "Test Score", "style": {"fontSize": "36px", "fontWeight": "bold"}}, |
| 69 | + "labels": {"style": {"fontSize": "28px"}}, |
| 70 | + "lineWidth": 2, |
| 71 | + "tickWidth": 2, |
| 72 | + "gridLineWidth": 1, |
| 73 | + "gridLineColor": "#e0e0e0", |
| 74 | +} |
| 75 | + |
| 76 | +# Y-axis |
| 77 | +chart.options.y_axis = { |
| 78 | + "title": {"text": "Probability Density", "style": {"fontSize": "36px", "fontWeight": "bold"}}, |
| 79 | + "labels": {"style": {"fontSize": "28px"}}, |
| 80 | + "gridLineWidth": 1, |
| 81 | + "gridLineColor": "#e0e0e0", |
| 82 | + "gridLineDashStyle": "Dash", |
| 83 | + "min": 0, |
| 84 | +} |
| 85 | + |
| 86 | +# Legend |
| 87 | +chart.options.legend = { |
| 88 | + "enabled": True, |
| 89 | + "itemStyle": {"fontSize": "28px", "fontWeight": "normal"}, |
| 90 | + "align": "right", |
| 91 | + "verticalAlign": "top", |
| 92 | + "layout": "vertical", |
| 93 | + "x": -50, |
| 94 | + "y": 100, |
| 95 | + "floating": True, |
| 96 | + "backgroundColor": "rgba(255, 255, 255, 0.9)", |
| 97 | + "borderWidth": 1, |
| 98 | + "borderColor": "#cccccc", |
| 99 | +} |
| 100 | + |
| 101 | +# Plot options |
| 102 | +chart.options.plot_options = { |
| 103 | + "column": {"groupPadding": 0, "pointPadding": 0, "borderWidth": 1, "borderColor": "#1a4d73"}, |
| 104 | + "areaspline": {"fillOpacity": 0.2, "lineWidth": 5, "marker": {"enabled": False}}, |
| 105 | +} |
| 106 | + |
| 107 | +# Add histogram series (using column chart) |
| 108 | +histogram_series = ColumnSeries() |
| 109 | +histogram_series.name = "Empirical Density" |
| 110 | +histogram_series.data = [[float(center), float(count)] for center, count in zip(bin_centers, counts, strict=True)] |
| 111 | +histogram_series.color = "#306998" |
| 112 | +histogram_series.point_width = bin_width * 35 # Adjust width for visibility |
| 113 | + |
| 114 | +chart.add_series(histogram_series) |
| 115 | + |
| 116 | +# Add theoretical PDF overlay as area spline |
| 117 | +pdf_series = AreaSplineSeries() |
| 118 | +pdf_series.name = "Normal PDF" |
| 119 | +pdf_series.data = [[float(x), float(y)] for x, y in zip(x_pdf, y_pdf, strict=True)] |
| 120 | +pdf_series.color = "#FFD43B" |
| 121 | +pdf_series.fill_color = "rgba(255, 212, 59, 0.2)" |
| 122 | +pdf_series.line_width = 5 |
| 123 | + |
| 124 | +chart.add_series(pdf_series) |
| 125 | + |
| 126 | +# Credits |
| 127 | +chart.options.credits = {"enabled": False} |
| 128 | + |
| 129 | +# Download Highcharts JS |
| 130 | +highcharts_url = "https://code.highcharts.com/highcharts.js" |
| 131 | +with urllib.request.urlopen(highcharts_url, timeout=30) as response: |
| 132 | + highcharts_js = response.read().decode("utf-8") |
| 133 | + |
| 134 | +# Generate HTML with inline scripts |
| 135 | +html_str = chart.to_js_literal() |
| 136 | +html_content = f"""<!DOCTYPE html> |
| 137 | +<html> |
| 138 | +<head> |
| 139 | + <meta charset="utf-8"> |
| 140 | + <script>{highcharts_js}</script> |
| 141 | +</head> |
| 142 | +<body style="margin:0;"> |
| 143 | + <div id="container" style="width: 4800px; height: 2700px;"></div> |
| 144 | + <script>{html_str}</script> |
| 145 | +</body> |
| 146 | +</html>""" |
| 147 | + |
| 148 | +# Write temp HTML and take screenshot |
| 149 | +with tempfile.NamedTemporaryFile(mode="w", suffix=".html", delete=False, encoding="utf-8") as f: |
| 150 | + f.write(html_content) |
| 151 | + temp_path = f.name |
| 152 | + |
| 153 | +# Also save as plot.html for interactive viewing |
| 154 | +with open("plot.html", "w", encoding="utf-8") as f: |
| 155 | + # For the HTML file, we can use CDN |
| 156 | + html_cdn = ( |
| 157 | + """<!DOCTYPE html> |
| 158 | +<html> |
| 159 | +<head> |
| 160 | + <meta charset="utf-8"> |
| 161 | + <script src="https://code.highcharts.com/highcharts.js"></script> |
| 162 | +</head> |
| 163 | +<body style="margin:0;"> |
| 164 | + <div id="container" style="width: 100%; height: 100vh;"></div> |
| 165 | + <script>""" |
| 166 | + + html_str |
| 167 | + + """</script> |
| 168 | +</body> |
| 169 | +</html>""" |
| 170 | + ) |
| 171 | + f.write(html_cdn) |
| 172 | + |
| 173 | +chrome_options = Options() |
| 174 | +chrome_options.add_argument("--headless") |
| 175 | +chrome_options.add_argument("--no-sandbox") |
| 176 | +chrome_options.add_argument("--disable-dev-shm-usage") |
| 177 | +chrome_options.add_argument("--disable-gpu") |
| 178 | +chrome_options.add_argument("--window-size=4800,2800") |
| 179 | + |
| 180 | +driver = webdriver.Chrome(options=chrome_options) |
| 181 | +driver.get(f"file://{temp_path}") |
| 182 | +time.sleep(5) |
| 183 | +driver.save_screenshot("plot.png") |
| 184 | +driver.quit() |
| 185 | + |
| 186 | +Path(temp_path).unlink() |
0 commit comments