Skip to content

Commit 8df91b8

Browse files
feat(seaborn): implement confusion-matrix (#2283)
## Implementation: `confusion-matrix` - seaborn Implements the **seaborn** version of `confusion-matrix`. **File:** `plots/confusion-matrix/implementations/seaborn.py` --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20526590019)* --------- 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 fcd772f commit 8df91b8

2 files changed

Lines changed: 88 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+
confusion-matrix: Confusion Matrix Heatmap
3+
Library: seaborn 0.13.2 | Python 3.13.11
4+
Quality: 92/100 | Created: 2025-12-26
5+
"""
6+
7+
import matplotlib.pyplot as plt
8+
import numpy as np
9+
import seaborn as sns
10+
11+
12+
# Data - Multi-class classification results for a sentiment analysis model
13+
class_names = ["Negative", "Neutral", "Positive", "Mixed"]
14+
15+
# Create realistic confusion matrix with strong diagonal (good model)
16+
# but with some systematic confusion patterns
17+
confusion_matrix = np.array(
18+
[
19+
[156, 12, 5, 8], # Negative: mostly correct, some confused with Neutral
20+
[18, 142, 15, 10], # Neutral: hardest to classify, confused with all
21+
[3, 8, 168, 6], # Positive: good accuracy
22+
[11, 14, 9, 125], # Mixed: often confused with Neutral
23+
]
24+
)
25+
26+
# Create figure (square format for symmetric matrix)
27+
fig, ax = plt.subplots(figsize=(12, 12))
28+
29+
# Create heatmap with annotations
30+
sns.heatmap(
31+
confusion_matrix,
32+
annot=True,
33+
fmt="d",
34+
cmap="Blues",
35+
xticklabels=class_names,
36+
yticklabels=class_names,
37+
square=True,
38+
linewidths=2,
39+
linecolor="white",
40+
cbar_kws={"shrink": 0.8, "label": "Count"},
41+
annot_kws={"size": 20, "weight": "bold"},
42+
ax=ax,
43+
)
44+
45+
# Style the colorbar
46+
cbar = ax.collections[0].colorbar
47+
cbar.ax.tick_params(labelsize=16)
48+
cbar.ax.set_ylabel("Count", fontsize=18, labelpad=15)
49+
50+
# Labels and title
51+
ax.set_xlabel("Predicted Label", fontsize=22, labelpad=15)
52+
ax.set_ylabel("True Label", fontsize=22, labelpad=15)
53+
ax.set_title("Sentiment Analysis Model · confusion-matrix · seaborn · pyplots.ai", fontsize=24, pad=20)
54+
55+
# Style tick labels
56+
ax.tick_params(axis="both", labelsize=18)
57+
58+
# Rotate x-axis labels for better readability
59+
plt.xticks(rotation=45, ha="right")
60+
plt.yticks(rotation=0)
61+
62+
plt.tight_layout()
63+
plt.savefig("plot.png", dpi=300, bbox_inches="tight")
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
library: seaborn
2+
specification_id: confusion-matrix
3+
created: '2025-12-26T17:36:54Z'
4+
updated: '2025-12-26T17:40:13Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20526590019
7+
issue: 0
8+
python_version: 3.13.11
9+
library_version: 0.13.2
10+
preview_url: https://storage.googleapis.com/pyplots-images/plots/confusion-matrix/seaborn/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/confusion-matrix/seaborn/plot_thumb.png
12+
preview_html: null
13+
quality_score: 92
14+
review:
15+
strengths:
16+
- Excellent visual clarity with well-sized annotations and contrasting text colors
17+
on cells
18+
- Properly follows specification with True/Predicted axis labels and Blues colormap
19+
- Realistic sentiment analysis scenario with believable confusion patterns showing
20+
systematic errors
21+
- Clean, readable code following KISS principles
22+
- Square format appropriate for symmetric confusion matrix
23+
weaknesses:
24+
- Minor colorbar label duplication (label set both in cbar_kws and manually)
25+
- Data is deterministic but no explicit np.random.seed() for documentation purposes

0 commit comments

Comments
 (0)