Skip to content

Commit e68c43e

Browse files
update(heatmap-basic): plotnine — comprehensive quality review
Comprehensive quality review: fix weaknesses from prior reviews, preserve strengths, improve quality across all dimensions.
1 parent 89f6f3b commit e68c43e

4 files changed

Lines changed: 50 additions & 42 deletions

File tree

plots/heatmap-basic/implementations/plotnine.py

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,82 @@
1-
""" pyplots.ai
1+
"""pyplots.ai
22
heatmap-basic: Basic Heatmap
3-
Library: plotnine 0.15.2 | Python 3.13.11
4-
Quality: 92/100 | Created: 2025-12-23
3+
Library: plotnine 0.15.3 | Python 3.14.3
4+
Quality: /100 | Updated: 2026-02-15
55
"""
66

77
import numpy as np
88
import pandas as pd
99
from plotnine import (
1010
aes,
1111
element_blank,
12+
element_rect,
1213
element_text,
1314
geom_text,
1415
geom_tile,
1516
ggplot,
1617
labs,
18+
scale_color_identity,
1719
scale_fill_gradient2,
20+
scale_x_discrete,
21+
scale_y_discrete,
1822
theme,
1923
theme_minimal,
2024
)
2125

2226

23-
# Data - 8x8 matrix with meaningful patterns (performance metrics by region and quarter)
27+
# Data - 8x8 matrix: quarterly growth rates (%) by department
2428
np.random.seed(42)
25-
rows = ["Region A", "Region B", "Region C", "Region D", "Region E", "Region F", "Region G", "Region H"]
26-
cols = ["Q1 2023", "Q2 2023", "Q3 2023", "Q4 2023", "Q1 2024", "Q2 2024", "Q3 2024", "Q4 2024"]
29+
departments = ["Engineering", "Marketing", "Sales", "Finance", "Operations", "HR", "Research", "Support"]
30+
quarters = ["Q1 '23", "Q2 '23", "Q3 '23", "Q4 '23", "Q1 '24", "Q2 '24", "Q3 '24", "Q4 '24"]
2731

28-
# Create data with a trend and variation
29-
base_values = np.linspace(-30, 40, 8) # Trend across columns
30-
row_effects = np.random.uniform(-10, 10, 8) # Row-specific offsets
32+
# Growth rates with a recovery trend and departmental variation
33+
base_trend = np.linspace(-15, 20, 8)
34+
dept_offsets = np.array([-5, 8, 12, -2, 3, -8, 6, -4])
3135
values = np.zeros((8, 8))
3236
for i in range(8):
3337
for j in range(8):
34-
values[i, j] = base_values[j] + row_effects[i] + np.random.uniform(-8, 8)
38+
values[i, j] = round(base_trend[j] + dept_offsets[i] + np.random.normal(0, 4), 1)
3539

36-
# Create long-form DataFrame for plotnine
37-
data = []
38-
for i, row in enumerate(rows):
39-
for j, col in enumerate(cols):
40-
data.append({"x": col, "y": row, "value": round(values[i, j], 1)})
40+
# Long-form DataFrame
41+
records = []
42+
for i, dept in enumerate(departments):
43+
for j, qtr in enumerate(quarters):
44+
records.append({"Department": dept, "Quarter": qtr, "Growth": values[i, j]})
4145

42-
df = pd.DataFrame(data)
46+
df = pd.DataFrame(records)
47+
df["Quarter"] = pd.Categorical(df["Quarter"], categories=quarters, ordered=True)
48+
df["Department"] = pd.Categorical(df["Department"], categories=departments[::-1], ordered=True)
4349

44-
# Preserve ordering
45-
df["x"] = pd.Categorical(df["x"], categories=cols, ordered=True)
46-
df["y"] = pd.Categorical(df["y"], categories=rows[::-1], ordered=True) # Reverse for top-to-bottom
50+
# Conditional text color: white on dark blue cells, dark on light cells
51+
df["text_color"] = df["Growth"].apply(lambda v: "white" if v < -10 else "#333333")
52+
53+
# Format labels with sign
54+
df["label"] = df["Growth"].apply(lambda v: f"{v:+.1f}")
4755

4856
# Plot
4957
plot = (
50-
ggplot(df, aes(x="x", y="y", fill="value"))
51-
+ geom_tile(color="white", size=0.5)
52-
+ geom_text(aes(label="value"), size=12, color="black")
53-
+ scale_fill_gradient2(
54-
low="#306998", # Python Blue for negative
55-
mid="white",
56-
high="#FFD43B", # Python Yellow for positive
57-
midpoint=0,
58-
name="Value",
59-
)
60-
+ labs(x="Time Period", y="Region", title="heatmap-basic · plotnine · pyplots.ai")
58+
ggplot(df, aes(x="Quarter", y="Department"))
59+
+ geom_tile(aes(fill="Growth"), color="white", size=0.8)
60+
+ geom_text(aes(label="label", color="text_color"), size=11, fontweight="bold", show_legend=False)
61+
+ scale_fill_gradient2(low="#306998", mid="#f5f5f0", high="#FFD43B", midpoint=0, name="Growth (%)")
62+
+ scale_color_identity()
63+
+ scale_x_discrete(expand=(0, 0.5))
64+
+ scale_y_discrete(expand=(0, 0.5))
65+
+ labs(x="", y="", title="Quarterly Growth by Department · heatmap-basic · plotnine · pyplots.ai")
6166
+ theme_minimal()
6267
+ theme(
6368
figure_size=(16, 9),
64-
text=element_text(size=14),
65-
axis_title=element_text(size=20),
66-
axis_text_x=element_text(size=14, rotation=45, ha="right"),
67-
axis_text_y=element_text(size=16),
68-
plot_title=element_text(size=24),
69+
text=element_text(family="sans-serif"),
70+
plot_title=element_text(size=22, ha="center", margin={"b": 15}),
71+
axis_text_x=element_text(size=16, rotation=45, ha="right", margin={"t": 5}),
72+
axis_text_y=element_text(size=16, ha="right", margin={"r": 5}),
6973
legend_title=element_text(size=16),
7074
legend_text=element_text(size=14),
75+
legend_position="right",
7176
panel_grid_major=element_blank(),
7277
panel_grid_minor=element_blank(),
78+
panel_background=element_rect(fill="white"),
79+
plot_background=element_rect(fill="white"),
7380
)
7481
)
7582

plots/heatmap-basic/metadata/plotnine.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
library: plotnine
22
specification_id: heatmap-basic
33
created: '2025-12-23T00:47:36Z'
4-
updated: '2025-12-23T00:51:16Z'
5-
generated_by: claude-opus-4-5-20251101
4+
updated: '2026-02-15T21:25:00+00:00'
5+
generated_by: claude-opus-4-6
66
workflow_run: 20447967616
77
issue: 0
8-
python_version: 3.13.11
9-
library_version: 0.15.2
8+
python_version: 3.14.3
9+
library_version: 0.15.3
1010
preview_url: https://storage.googleapis.com/pyplots-images/plots/heatmap-basic/plotnine/plot.png
1111
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/heatmap-basic/plotnine/plot_thumb.png
1212
preview_html: null
13-
quality_score: 92
13+
quality_score: null
1414
impl_tags:
1515
dependencies: []
1616
techniques:

plots/heatmap-basic/specification.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ A heatmap displaying values in a matrix format using color intensity. Each cell'
2323
- Use a diverging colormap for data with positive/negative values
2424
- Add value annotations in cells when readable
2525
- Include a colorbar legend
26-
- Consider clustering rows/columns for better pattern visibility
26+
- Order rows/columns logically (alphabetical, by magnitude, or by similarity)

plots/heatmap-basic/specification.yaml

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

77
# Specification tracking
88
created: 2025-12-14T09:02:34Z
9-
updated: 2025-12-14T09:02:34Z
9+
updated: 2026-02-15T12:00:00Z
1010
issue: 691
1111
suggested: MarkusNeusinger
1212

@@ -18,6 +18,7 @@ tags:
1818
data_type:
1919
- numeric
2020
- categorical
21+
- matrix
2122
domain:
2223
- statistics
2324
- general

0 commit comments

Comments
 (0)