|
1 | 1 | """ pyplots.ai |
2 | 2 | density-basic: Basic Density Plot |
3 | | -Library: highcharts unknown | Python 3.13.11 |
4 | | -Quality: 91/100 | Created: 2025-12-23 |
| 3 | +Library: highcharts 1.10.3 | Python 3.14 |
| 4 | +Quality: /100 | Updated: 2026-02-23 |
5 | 5 | """ |
6 | 6 |
|
7 | 7 | import tempfile |
|
18 | 18 | from selenium.webdriver.chrome.options import Options |
19 | 19 |
|
20 | 20 |
|
21 | | -# Data - simulating heights (cm) with realistic distribution |
| 21 | +# Data - simulating heights (cm) with bimodal distribution |
22 | 22 | np.random.seed(42) |
23 | | -# Mix of two normal distributions to show bimodal feature (male/female heights) |
24 | 23 | n_samples = 500 |
25 | | -values_a = np.random.normal(165, 7, n_samples // 2) # Female heights |
26 | | -values_b = np.random.normal(178, 8, n_samples // 2) # Male heights |
| 24 | +values_a = np.random.normal(162, 6, n_samples // 2) # Female heights |
| 25 | +values_b = np.random.normal(178, 6, n_samples // 2) # Male heights |
27 | 26 | values = np.concatenate([values_a, values_b]) |
28 | 27 |
|
29 | | -# Kernel Density Estimation (Gaussian kernel) - inline calculation |
30 | | -x_min, x_max = values.min() - 10, values.max() + 10 |
31 | | -x_grid = np.linspace(x_min, x_max, 200) |
| 28 | +# Kernel Density Estimation (Gaussian kernel) |
| 29 | +x_min, x_max = values.min() - 12, values.max() + 12 |
| 30 | +x_grid = np.linspace(x_min, x_max, 300) |
32 | 31 |
|
33 | 32 | # Silverman's rule of thumb for bandwidth |
34 | 33 | n = len(values) |
35 | | -bandwidth = 1.06 * np.std(values) * n ** (-1 / 5) |
| 34 | +bandwidth = 0.9 * min(np.std(values), np.subtract(*np.percentile(values, [75, 25])) / 1.34) * n ** (-1 / 5) |
36 | 35 |
|
37 | 36 | # Compute Gaussian KDE |
38 | 37 | density = np.zeros_like(x_grid) |
|
50 | 49 | "width": 4800, |
51 | 50 | "height": 2700, |
52 | 51 | "backgroundColor": "#ffffff", |
53 | | - "marginBottom": 200, |
54 | | - "marginLeft": 200, |
| 52 | + "marginBottom": 180, |
| 53 | + "marginLeft": 180, |
| 54 | + "marginRight": 60, |
| 55 | + "marginTop": 140, |
| 56 | + "style": {"fontFamily": "Arial, Helvetica, sans-serif"}, |
55 | 57 | } |
56 | 58 |
|
57 | 59 | # Title |
58 | 60 | chart.options.title = { |
59 | | - "text": "density-basic · highcharts · pyplots.ai", |
60 | | - "style": {"fontSize": "72px", "fontWeight": "bold"}, |
| 61 | + "text": "density-basic \u00b7 highcharts \u00b7 pyplots.ai", |
| 62 | + "style": {"fontSize": "64px", "fontWeight": "600", "color": "#2c3e50"}, |
| 63 | + "margin": 40, |
61 | 64 | } |
62 | 65 |
|
63 | | -# X-axis |
| 66 | +# Disable credits |
| 67 | +chart.options.credits = {"enabled": False} |
| 68 | + |
| 69 | +# X-axis - clean L-shaped frame |
64 | 70 | chart.options.x_axis = { |
65 | | - "title": {"text": "Height (cm)", "style": {"fontSize": "48px"}}, |
66 | | - "labels": {"style": {"fontSize": "36px"}}, |
67 | | - "gridLineWidth": 1, |
68 | | - "gridLineColor": "rgba(0, 0, 0, 0.25)", |
| 71 | + "title": {"text": "Height (cm)", "style": {"fontSize": "48px", "color": "#444444"}, "margin": 24}, |
| 72 | + "labels": {"style": {"fontSize": "36px", "color": "#666666"}}, |
| 73 | + "lineColor": "#cccccc", |
| 74 | + "lineWidth": 2, |
| 75 | + "tickWidth": 0, |
| 76 | + "tickInterval": 5, |
| 77 | + "gridLineWidth": 0, |
69 | 78 | } |
70 | 79 |
|
71 | | -# Y-axis - start at 0 for proper density representation |
| 80 | +# Y-axis - subtle horizontal grid only |
72 | 81 | chart.options.y_axis = { |
73 | | - "title": {"text": "Density", "style": {"fontSize": "48px"}}, |
74 | | - "labels": {"style": {"fontSize": "36px"}}, |
| 82 | + "title": {"text": "Density", "style": {"fontSize": "48px", "color": "#444444"}, "margin": 24}, |
| 83 | + "labels": {"style": {"fontSize": "36px", "color": "#666666"}}, |
75 | 84 | "gridLineWidth": 1, |
76 | | - "gridLineColor": "rgba(0, 0, 0, 0.25)", |
| 85 | + "gridLineColor": "rgba(0, 0, 0, 0.10)", |
| 86 | + "lineColor": "#cccccc", |
| 87 | + "lineWidth": 2, |
| 88 | + "tickAmount": 7, |
77 | 89 | "min": 0, |
78 | 90 | } |
79 | 91 |
|
80 | | -# Plot options with semi-transparent fill |
| 92 | +# Plot options |
81 | 93 | chart.options.plot_options = { |
82 | 94 | "area": { |
83 | 95 | "fillColor": { |
84 | 96 | "linearGradient": {"x1": 0, "y1": 0, "x2": 0, "y2": 1}, |
85 | | - "stops": [[0, "rgba(48, 105, 152, 0.6)"], [1, "rgba(48, 105, 152, 0.1)"]], |
| 97 | + "stops": [[0, "rgba(48, 105, 152, 0.45)"], [1, "rgba(48, 105, 152, 0.03)"]], |
86 | 98 | }, |
87 | 99 | "lineWidth": 5, |
88 | 100 | "marker": {"enabled": False}, |
89 | 101 | "color": "#306998", |
| 102 | + "states": {"hover": {"lineWidth": 5}}, |
90 | 103 | }, |
91 | 104 | "scatter": { |
92 | | - "marker": {"radius": 8, "fillColor": "#FFD43B", "symbol": "diamond", "lineWidth": 2, "lineColor": "#D4AA00"} |
| 105 | + "marker": {"radius": 5, "fillColor": "rgba(48, 105, 152, 0.6)", "symbol": "diamond", "lineWidth": 0}, |
| 106 | + "states": {"hover": {"enabled": False}}, |
93 | 107 | }, |
| 108 | + "series": {"animation": False}, |
94 | 109 | } |
95 | 110 |
|
96 | | -# Legend - enabled with clear styling |
| 111 | +# Disable tooltip for static export |
| 112 | +chart.options.tooltip = {"enabled": False} |
| 113 | + |
| 114 | +# Legend |
97 | 115 | chart.options.legend = { |
98 | 116 | "enabled": True, |
99 | 117 | "layout": "horizontal", |
100 | 118 | "align": "right", |
101 | 119 | "verticalAlign": "top", |
102 | 120 | "floating": True, |
103 | | - "x": -50, |
104 | | - "y": 80, |
105 | | - "itemStyle": {"fontSize": "36px"}, |
106 | | - "symbolHeight": 24, |
107 | | - "symbolWidth": 40, |
| 121 | + "x": -40, |
| 122 | + "y": 60, |
| 123 | + "itemStyle": {"fontSize": "34px", "fontWeight": "normal", "color": "#555555"}, |
| 124 | + "symbolHeight": 20, |
| 125 | + "symbolWidth": 32, |
| 126 | + "itemDistance": 40, |
| 127 | + "borderWidth": 0, |
108 | 128 | } |
109 | 129 |
|
110 | 130 | # Add density curve as area series |
111 | 131 | area_series = AreaSeries() |
112 | | -area_series.data = [[float(x), float(y)] for x, y in zip(x_grid, density, strict=True)] |
113 | | -area_series.name = "Density Curve" |
| 132 | +area_series.data = [[round(float(x), 2), round(float(y), 6)] for x, y in zip(x_grid, density, strict=True)] |
| 133 | +area_series.name = "Density" |
114 | 134 | chart.add_series(area_series) |
115 | 135 |
|
116 | | -# Add rug plot as small vertical tick marks at y=0 |
117 | | -# Sample every 5th point to show distribution without overcrowding |
118 | | -rug_sample = values[::5] |
119 | | -rug_y = 0.0005 # Small positive value just above x-axis |
120 | | -rug_data = [[float(v), rug_y] for v in sorted(rug_sample)] |
| 136 | +# Add rug plot - vertical tick marks along x-axis |
| 137 | +rug_sample = values[::3] # Show every 3rd observation for good coverage |
| 138 | +rug_y = max(density) * 0.008 # Small positive value near axis |
| 139 | +rug_data = [[round(float(v), 2), round(float(rug_y), 6)] for v in sorted(rug_sample)] |
121 | 140 |
|
122 | 141 | rug_series = ScatterSeries() |
123 | 142 | rug_series.data = rug_data |
124 | | -rug_series.name = "Observations (Rug)" |
125 | | -rug_series.marker = {"symbol": "diamond", "fillColor": "#FFD43B", "lineColor": "#D4AA00", "lineWidth": 2, "radius": 10} |
| 143 | +rug_series.name = "Observations" |
| 144 | +rug_series.marker = {"symbol": "diamond", "fillColor": "rgba(48, 105, 152, 0.55)", "lineWidth": 0, "radius": 10} |
126 | 145 | chart.add_series(rug_series) |
127 | 146 |
|
128 | 147 | # Download Highcharts JS for inline embedding |
|
149 | 168 | f.write(html_content) |
150 | 169 | temp_path = f.name |
151 | 170 |
|
152 | | -# Also save HTML for interactive version |
| 171 | +# Save standalone HTML for interactive version |
153 | 172 | with open("plot.html", "w", encoding="utf-8") as f: |
154 | | - # For standalone HTML, use CDN link |
155 | 173 | standalone_html = f"""<!DOCTYPE html> |
156 | 174 | <html> |
157 | 175 | <head> |
|
170 | 188 | chrome_options.add_argument("--no-sandbox") |
171 | 189 | chrome_options.add_argument("--disable-dev-shm-usage") |
172 | 190 | chrome_options.add_argument("--disable-gpu") |
173 | | -chrome_options.add_argument("--window-size=5000,3000") |
| 191 | +chrome_options.add_argument("--window-size=4900,2800") |
174 | 192 |
|
175 | 193 | driver = webdriver.Chrome(options=chrome_options) |
176 | 194 | driver.get(f"file://{temp_path}") |
177 | | -time.sleep(5) # Wait for chart to render |
| 195 | +time.sleep(5) |
178 | 196 |
|
179 | | -# Screenshot the chart element specifically for exact 4800x2700 |
180 | 197 | container = driver.find_element("id", "container") |
181 | 198 | container.screenshot("plot.png") |
182 | 199 | driver.quit() |
183 | 200 |
|
184 | | -Path(temp_path).unlink() # Clean up temp file |
| 201 | +Path(temp_path).unlink() |
0 commit comments