Skip to content

Commit fb1e7cd

Browse files
update(dendrogram-basic): matplotlib — comprehensive quality review
Comprehensive review improving code quality, data choice, visual design, spec compliance, and library feature usage.
1 parent f514b51 commit fb1e7cd

4 files changed

Lines changed: 62 additions & 42 deletions

File tree

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
1-
""" pyplots.ai
1+
"""pyplots.ai
22
dendrogram-basic: Basic Dendrogram
3-
Library: matplotlib 3.10.8 | Python 3.13.11
4-
Quality: 91/100 | Created: 2025-12-23
3+
Library: matplotlib 3.10.8 | Python 3.14.3
4+
Quality: /100 | Updated: 2026-04-05
55
"""
66

77
import matplotlib.pyplot as plt
88
import numpy as np
9-
from scipy.cluster.hierarchy import dendrogram, linkage
9+
from matplotlib.collections import LineCollection
10+
from scipy.cluster.hierarchy import dendrogram, linkage, set_link_color_palette
1011

1112

1213
# Data - Iris flower measurements (4 features for 15 samples)
1314
np.random.seed(42)
1415

15-
# Simulate iris-like measurements: sepal length, sepal width, petal length, petal width
16-
# Three species with distinct characteristics
1716
samples_per_species = 5
18-
1917
labels = []
2018
data = []
2119

@@ -24,10 +22,10 @@
2422
labels.append(f"Setosa-{i + 1}")
2523
data.append(
2624
[
27-
5.0 + np.random.randn() * 0.3, # sepal length
28-
3.4 + np.random.randn() * 0.3, # sepal width
29-
1.5 + np.random.randn() * 0.2, # petal length
30-
0.3 + np.random.randn() * 0.1, # petal width
25+
5.0 + np.random.randn() * 0.3,
26+
3.4 + np.random.randn() * 0.3,
27+
1.5 + np.random.randn() * 0.2,
28+
0.3 + np.random.randn() * 0.1,
3129
]
3230
)
3331

@@ -36,10 +34,10 @@
3634
labels.append(f"Versicolor-{i + 1}")
3735
data.append(
3836
[
39-
5.9 + np.random.randn() * 0.4, # sepal length
40-
2.8 + np.random.randn() * 0.3, # sepal width
41-
4.3 + np.random.randn() * 0.4, # petal length
42-
1.3 + np.random.randn() * 0.2, # petal width
37+
5.9 + np.random.randn() * 0.4,
38+
2.8 + np.random.randn() * 0.3,
39+
4.3 + np.random.randn() * 0.4,
40+
1.3 + np.random.randn() * 0.2,
4341
]
4442
)
4543

@@ -48,10 +46,10 @@
4846
labels.append(f"Virginica-{i + 1}")
4947
data.append(
5048
[
51-
6.6 + np.random.randn() * 0.5, # sepal length
52-
3.0 + np.random.randn() * 0.3, # sepal width
53-
5.5 + np.random.randn() * 0.5, # petal length
54-
2.0 + np.random.randn() * 0.3, # petal width
49+
6.6 + np.random.randn() * 0.5,
50+
3.0 + np.random.randn() * 0.3,
51+
5.5 + np.random.randn() * 0.5,
52+
2.0 + np.random.randn() * 0.3,
5553
]
5654
)
5755

@@ -61,33 +59,53 @@
6159
linkage_matrix = linkage(data, method="ward")
6260

6361
# Plot
64-
fig, ax = plt.subplots(figsize=(16, 9))
62+
fig, ax = plt.subplots(figsize=(16, 9), facecolor="white")
63+
ax.set_facecolor("#FAFAFA")
64+
65+
# Custom cluster colors via set_link_color_palette (matplotlib/scipy integration)
66+
cluster_colors = ["#306998", "#D4722A", "#3A8A5C"]
67+
set_link_color_palette(cluster_colors)
6568

66-
# Create dendrogram with custom colors
67-
dendrogram(
69+
dendro = dendrogram(
6870
linkage_matrix,
6971
labels=labels,
7072
ax=ax,
71-
leaf_rotation=45,
72-
leaf_font_size=14,
73-
above_threshold_color="#306998", # Python Blue for main branches
74-
color_threshold=0.7 * max(linkage_matrix[:, 2]), # Color threshold for clusters
73+
leaf_rotation=35,
74+
leaf_font_size=15,
75+
above_threshold_color="#AAAAAA",
76+
color_threshold=0.7 * max(linkage_matrix[:, 2]),
7577
)
7678

79+
# Post-render enhancement: adjust line widths via LineCollection traversal
80+
for child in ax.get_children():
81+
if isinstance(child, LineCollection):
82+
child.set_linewidths(3.0)
83+
child.set_capstyle("round")
84+
child.set_joinstyle("round")
85+
7786
# Style
78-
ax.set_xlabel("Sample", fontsize=20)
79-
ax.set_ylabel("Distance (Ward)", fontsize=20)
80-
ax.set_title("dendrogram-basic · matplotlib · pyplots.ai", fontsize=24)
81-
ax.tick_params(axis="both", labelsize=16)
82-
ax.tick_params(axis="x", labelsize=14, rotation=45)
87+
ax.set_xlabel("Iris Sample", fontsize=20, labelpad=10)
88+
ax.set_ylabel("Ward Linkage Distance", fontsize=20, labelpad=10)
89+
ax.set_title(
90+
"Iris Species Clustering · dendrogram-basic · matplotlib · pyplots.ai",
91+
fontsize=24,
92+
fontweight="medium",
93+
pad=20,
94+
color="#333333",
95+
)
96+
ax.tick_params(axis="both", labelsize=16, colors="#555555")
97+
ax.tick_params(axis="x", labelsize=15, rotation=35)
8398

84-
# Adjust spines for cleaner look
8599
ax.spines["top"].set_visible(False)
86100
ax.spines["right"].set_visible(False)
101+
ax.spines["left"].set_linewidth(0.6)
102+
ax.spines["left"].set_color("#CCCCCC")
103+
ax.spines["bottom"].set_linewidth(0.6)
104+
ax.spines["bottom"].set_color("#CCCCCC")
87105

88-
# Add subtle grid on y-axis only
89-
ax.yaxis.grid(True, alpha=0.3, linestyle="--")
106+
# Subtle grid on y-axis only
107+
ax.yaxis.grid(True, alpha=0.15, linewidth=0.6, color="#888888")
90108
ax.set_axisbelow(True)
91109

92-
plt.tight_layout()
93-
plt.savefig("plot.png", dpi=300, bbox_inches="tight")
110+
plt.tight_layout(pad=1.5)
111+
plt.savefig("plot.png", dpi=300, bbox_inches="tight", facecolor="white")

plots/dendrogram-basic/metadata/matplotlib.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
library: matplotlib
22
specification_id: dendrogram-basic
33
created: '2025-12-23T10:00:50Z'
4-
updated: '2025-12-23T10:06:28Z'
5-
generated_by: claude-opus-4-5-20251101
4+
updated: '2026-04-05T20:00:00+00:00'
5+
generated_by: claude-opus-4-6
66
workflow_run: 20457531070
77
issue: 0
8-
python_version: 3.13.11
8+
python_version: '3.14.3'
99
library_version: 3.10.8
1010
preview_url: https://storage.googleapis.com/pyplots-images/plots/dendrogram-basic/matplotlib/plot.png
1111
preview_html: null
12-
quality_score: 91
12+
quality_score: null
1313
impl_tags:
1414
dependencies:
1515
- scipy

plots/dendrogram-basic/specification.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ A dendrogram visualizes hierarchical clustering by showing how data points or cl
1313

1414
## Data
1515

16+
- `features` (numeric matrix) - measurement values for each item (e.g., petal length, sepal width), used to compute distances
1617
- `labels` (string) - names or identifiers for each item being clustered
17-
- `linkage_matrix` (numeric) - output from scipy's linkage function containing merge distances
18+
- `linkage_matrix` (numeric) - output from scipy's linkage function containing merge distances, computed from features
1819
- Size: 10-50 items recommended for readable dendrograms
1920
- Example: hierarchical clustering of iris flower species by measurements
2021

plots/dendrogram-basic/specification.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ title: Basic Dendrogram
66

77
# Specification tracking
88
created: 2025-12-15T20:43:34Z
9-
updated: 2025-12-15T20:43:34Z
9+
updated: 2026-04-05T12:00:00Z
1010
issue: 986
1111
suggested: MarkusNeusinger
1212

@@ -17,6 +17,7 @@ tags:
1717
- tree
1818
data_type:
1919
- hierarchical
20+
- numeric
2021
domain:
2122
- statistics
2223
- science

0 commit comments

Comments
 (0)