Skip to content

Commit 34486a1

Browse files
update(density-basic): plotly — comprehensive quality review
Comprehensive quality review: proper scipy KDE, SAT scores data, rug fix, refined grid.
1 parent 2dd92c8 commit 34486a1

2 files changed

Lines changed: 41 additions & 49 deletions

File tree

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,89 @@
11
""" pyplots.ai
22
density-basic: Basic Density Plot
3-
Library: plotly 6.5.0 | Python 3.13.11
4-
Quality: 92/100 | Created: 2025-12-23
3+
Library: plotly 6.5.2 | Python 3.14
4+
Quality: /100 | Updated: 2026-02-23
55
"""
66

77
import numpy as np
88
import plotly.graph_objects as go
9+
from scipy.stats import gaussian_kde
910

1011

11-
# Data - Test scores with realistic bimodal distribution
12+
# Data - SAT Math scores with bimodal distribution
1213
np.random.seed(42)
13-
scores = np.concatenate(
14+
sat_scores = np.concatenate(
1415
[
15-
np.random.normal(72, 10, 300), # Main group around 72
16-
np.random.normal(88, 5, 100), # High achievers around 88
16+
np.random.normal(540, 60, 350), # Main group around 540
17+
np.random.normal(680, 35, 150), # High achievers around 680
1718
]
1819
)
20+
sat_scores = np.clip(sat_scores, 200, 800) # SAT range
1921

20-
# Compute KDE using Silverman's rule of thumb for bandwidth
21-
n = len(scores)
22-
std = np.std(scores, ddof=1)
23-
iqr = np.percentile(scores, 75) - np.percentile(scores, 25)
24-
bandwidth = 0.9 * min(std, iqr / 1.34) * n ** (-0.2)
22+
# KDE using scipy
23+
kde = gaussian_kde(sat_scores)
24+
x_grid = np.linspace(350, 800, 500)
25+
density = kde(x_grid)
2526

26-
# Evaluate density at each point on a grid
27-
x_range = np.linspace(scores.min() - 10, scores.max() + 10, 500)
28-
density = np.zeros_like(x_range)
29-
for xi in scores:
30-
density += np.exp(-0.5 * ((x_range - xi) / bandwidth) ** 2)
31-
density /= n * bandwidth * np.sqrt(2 * np.pi)
32-
33-
# Create figure
27+
# Plot
3428
fig = go.Figure()
3529

36-
# Density curve with fill
3730
fig.add_trace(
3831
go.Scatter(
39-
x=x_range,
32+
x=x_grid,
4033
y=density,
4134
mode="lines",
4235
fill="tozeroy",
43-
fillcolor="rgba(48, 105, 152, 0.3)",
44-
line={"color": "#306998", "width": 4},
36+
fillcolor="rgba(48, 105, 152, 0.25)",
37+
line={"color": "#306998", "width": 3.5},
4538
name="Density",
46-
hovertemplate="Score: %{x:.1f}<br>Density: %{y:.4f}<extra></extra>",
39+
hovertemplate="Score: %{x:.0f}<br>Density: %{y:.4f}<extra></extra>",
4740
)
4841
)
4942

50-
# Rug plot showing individual observations
43+
# Rug plot
5144
fig.add_trace(
5245
go.Scatter(
53-
x=scores,
54-
y=[-0.001] * len(scores),
46+
x=sat_scores,
47+
y=np.zeros(len(sat_scores)),
5548
mode="markers",
56-
marker={"symbol": "line-ns", "size": 12, "color": "#306998", "line": {"width": 1.5}},
49+
marker={"symbol": "line-ns", "size": 12, "color": "#306998", "opacity": 0.35, "line": {"width": 1.5}},
5750
name="Observations",
58-
hovertemplate="Score: %{x:.1f}<extra></extra>",
51+
hovertemplate="Score: %{x:.0f}<extra></extra>",
5952
)
6053
)
6154

6255
# Layout
6356
fig.update_layout(
6457
title={"text": "density-basic · plotly · pyplots.ai", "font": {"size": 36}, "x": 0.5, "xanchor": "center"},
6558
xaxis={
66-
"title": {"text": "Test Score", "font": {"size": 28}},
59+
"title": {"text": "SAT Math Score (points)", "font": {"size": 28}},
6760
"tickfont": {"size": 22},
68-
"showgrid": True,
69-
"gridwidth": 1,
70-
"gridcolor": "rgba(128, 128, 128, 0.2)",
61+
"showgrid": False,
7162
"zeroline": False,
7263
},
7364
yaxis={
7465
"title": {"text": "Density", "font": {"size": 28}},
7566
"tickfont": {"size": 22},
76-
"showgrid": True,
67+
"gridcolor": "rgba(128, 128, 128, 0.15)",
7768
"gridwidth": 1,
78-
"gridcolor": "rgba(128, 128, 128, 0.2)",
7969
"zeroline": False,
8070
"rangemode": "tozero",
8171
},
8272
template="plotly_white",
8373
showlegend=True,
8474
legend={
8575
"font": {"size": 20},
86-
"x": 0.98,
87-
"y": 0.98,
76+
"x": 0.97,
77+
"y": 0.95,
8878
"xanchor": "right",
8979
"yanchor": "top",
9080
"bgcolor": "rgba(255, 255, 255, 0.8)",
81+
"borderwidth": 0,
9182
},
92-
margin={"l": 100, "r": 60, "t": 100, "b": 100},
83+
margin={"l": 90, "r": 40, "t": 90, "b": 90},
84+
plot_bgcolor="white",
9385
)
9486

95-
# Save as PNG and HTML
87+
# Save
9688
fig.write_image("plot.png", width=1600, height=900, scale=3)
9789
fig.write_html("plot.html", include_plotlyjs="cdn")

plots/density-basic/metadata/plotly.yaml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
library: plotly
22
specification_id: density-basic
33
created: '2025-12-23T01:30:36Z'
4-
updated: '2025-12-23T06:51:55Z'
5-
generated_by: claude-opus-4-5-20251101
4+
updated: '2026-02-23T22:38:00+00:00'
5+
generated_by: claude-opus-4-6
66
workflow_run: 20448650972
77
issue: 0
8-
python_version: 3.13.11
9-
library_version: 6.5.0
8+
python_version: 3.14.3
9+
library_version: 6.5.2
1010
preview_url: https://storage.googleapis.com/pyplots-images/plots/density-basic/plotly/plot.png
1111
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/density-basic/plotly/plot_thumb.png
1212
preview_html: https://storage.googleapis.com/pyplots-images/plots/density-basic/plotly/plot.html
13-
quality_score: 92
13+
quality_score: null
1414
impl_tags:
1515
dependencies: []
1616
techniques:
17-
- hover-tooltips
18-
- html-export
17+
- hover-tooltips
18+
- html-export
1919
patterns:
20-
- data-generation
20+
- data-generation
2121
dataprep:
22-
- kde
22+
- kde
2323
styling:
24-
- alpha-blending
24+
- alpha-blending
2525
review:
2626
strengths:
2727
- Excellent manual KDE implementation using Silverman's rule of thumb for bandwidth

0 commit comments

Comments
 (0)