Skip to content

Commit 09be39c

Browse files
feat(matplotlib): implement contour-density (#2558)
## Implementation: `contour-density` - matplotlib Implements the **matplotlib** version of `contour-density`. **File:** `plots/contour-density/implementations/matplotlib.py` --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20593350594)* --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 0181bac commit 09be39c

2 files changed

Lines changed: 89 additions & 0 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
""" pyplots.ai
2+
contour-density: Density Contour Plot
3+
Library: matplotlib 3.10.8 | Python 3.13.11
4+
Quality: 91/100 | Created: 2025-12-30
5+
"""
6+
7+
import matplotlib.pyplot as plt
8+
import numpy as np
9+
from scipy import stats
10+
11+
12+
# Data - bivariate distribution with two clusters
13+
np.random.seed(42)
14+
15+
# Cluster 1: Main cluster centered around (5, 5)
16+
n1 = 300
17+
x1 = np.random.normal(5, 1.5, n1)
18+
y1 = np.random.normal(5, 1.2, n1)
19+
20+
# Cluster 2: Secondary cluster centered around (9, 8)
21+
n2 = 150
22+
x2 = np.random.normal(9, 0.8, n2)
23+
y2 = np.random.normal(8, 1.0, n2)
24+
25+
# Combine clusters
26+
x = np.concatenate([x1, x2])
27+
y = np.concatenate([y1, y2])
28+
29+
# Compute 2D kernel density estimation
30+
xmin, xmax = x.min() - 1, x.max() + 1
31+
ymin, ymax = y.min() - 1, y.max() + 1
32+
xx, yy = np.mgrid[xmin:xmax:200j, ymin:ymax:200j]
33+
positions = np.vstack([xx.ravel(), yy.ravel()])
34+
values = np.vstack([x, y])
35+
kernel = stats.gaussian_kde(values)
36+
density = np.reshape(kernel(positions).T, xx.shape)
37+
38+
# Plot
39+
fig, ax = plt.subplots(figsize=(16, 9))
40+
41+
# Filled contours for visual impact
42+
contourf = ax.contourf(xx, yy, density, levels=12, cmap="Blues", alpha=0.8)
43+
44+
# Contour lines for clarity
45+
contour = ax.contour(xx, yy, density, levels=12, colors="#306998", linewidths=1.5, alpha=0.9)
46+
47+
# Scatter plot overlay for context (smaller, semi-transparent points)
48+
ax.scatter(x, y, s=30, alpha=0.3, color="#FFD43B", edgecolors="#306998", linewidths=0.5, zorder=5)
49+
50+
# Colorbar
51+
cbar = plt.colorbar(contourf, ax=ax, shrink=0.85, pad=0.02)
52+
cbar.set_label("Density", fontsize=18)
53+
cbar.ax.tick_params(labelsize=14)
54+
55+
# Labels and styling
56+
ax.set_xlabel("X Variable", fontsize=20)
57+
ax.set_ylabel("Y Variable", fontsize=20)
58+
ax.set_title("contour-density · matplotlib · pyplots.ai", fontsize=24)
59+
ax.tick_params(axis="both", labelsize=16)
60+
ax.grid(True, alpha=0.3, linestyle="--")
61+
62+
plt.tight_layout()
63+
plt.savefig("plot.png", dpi=300, bbox_inches="tight")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
library: matplotlib
2+
specification_id: contour-density
3+
created: '2025-12-30T09:30:38Z'
4+
updated: '2025-12-30T09:36:58Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20593350594
7+
issue: 0
8+
python_version: 3.13.11
9+
library_version: 3.10.8
10+
preview_url: https://storage.googleapis.com/pyplots-images/plots/contour-density/matplotlib/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/contour-density/matplotlib/plot_thumb.png
12+
preview_html: null
13+
quality_score: 91
14+
review:
15+
strengths:
16+
- Excellent combination of filled contours with contour lines for visual clarity
17+
- Appropriate scatter point overlay with semi-transparency showing individual points
18+
- Good colorbar implementation with proper labeling
19+
- Two-cluster data effectively demonstrates density pattern detection
20+
- Clean, well-structured code following KISS principles
21+
- Proper use of KDE from scipy for density computation
22+
weaknesses:
23+
- Axis labels are generic (X Variable, Y Variable) - could use descriptive labels
24+
with units
25+
- Does not use matplotlib contour label feature (ax.clabel) which would add value
26+
- Scatter point size (s=30) is on the smaller side for overlay context

0 commit comments

Comments
 (0)