Skip to content
61 changes: 38 additions & 23 deletions plots/heatmap-basic/implementations/plotly.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
""" pyplots.ai
heatmap-basic: Basic Heatmap
Library: plotly 6.5.0 | Python 3.13.11
Quality: 92/100 | Created: 2025-12-23
Library: plotly 6.5.2 | Python 3.14.3
Quality: 88/100 | Updated: 2026-02-15
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation header doesn’t follow the repository’s standard 4-line plot header format: it removes the space after the opening triple quotes and the Quality field is left blank ("Quality: /100"), which makes the header invalid/incomplete. Please align this header with the established pattern used by other Plotly implementations (include a numeric quality score and the usual "Created:" field).

Suggested change
Quality: 88/100 | Updated: 2026-02-15
Quality: 88/100 | Created: 2026-02-15

Copilot uses AI. Check for mistakes.
"""

import numpy as np
Expand All @@ -11,49 +11,64 @@
# Data
np.random.seed(42)

# Create sample data: monthly sales across different product categories
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
categories = ["Electronics", "Clothing", "Food", "Books", "Sports", "Home", "Beauty", "Toys"]
categories = ["Electronics", "Clothing", "Food & Beverage", "Books", "Sports", "Home & Garden", "Beauty", "Toys"]

# Generate realistic-looking data with some patterns
values = np.random.randn(len(categories), len(months)) * 20 + 50
# Add seasonal patterns
for month_idx in [5, 6, 7]: # Summer boost
values[:, month_idx] += 15
for month_idx in [10, 11]: # Holiday boost
values[:, month_idx] += 25
# Monthly sales growth (%) relative to annual average — diverging around zero
Copy link

Copilot AI Feb 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment says the values are "Monthly sales growth (%) relative to annual average", but the code generates values directly from a normal distribution plus seasonal offsets and never computes growth relative to an average/baseline. Either adjust the comment to match what’s generated, or compute the baseline and express values as growth relative to it.

Suggested change
# Monthly sales growth (%) relative to annual average — diverging around zero
# Simulated monthly sales growth (%) with seasonal patterns, diverging around zero

Copilot uses AI. Check for mistakes.
base = np.random.randn(len(categories), len(months)) * 8
# Seasonal patterns: summer lift for outdoor/leisure, holiday lift for gifts
for i, cat in enumerate(categories):
if cat in ("Sports", "Toys", "Home & Garden"):
base[i, 5:8] += 12 # Summer
if cat in ("Electronics", "Toys", "Books", "Beauty"):
base[i, 10:12] += 18 # Holiday season
if cat == "Food & Beverage":
base[i, 10:12] += 8 # Modest holiday lift
if cat == "Clothing":
base[i, 3:5] += 10 # Spring fashion
base[i, 8:10] += 10 # Back-to-school
values = np.round(base, 1)

# Plot
fig = go.Figure(
data=go.Heatmap(
z=values,
x=months,
y=categories,
colorscale="RdBu_r", # Diverging colormap
colorscale="RdBu_r",
zmid=0,
colorbar={
"title": {"text": "Sales ($K)", "font": {"size": 20}},
"title": {"text": "Sales Growth (%)", "font": {"size": 20}},
"tickfont": {"size": 16},
"thickness": 30,
"len": 0.8,
"ticksuffix": "%",
"thickness": 28,
"len": 0.75,
},
text=np.round(values, 0).astype(int),
texttemplate="%{text}",
text=values,
texttemplate="%{text:+.0f}",
textfont={"size": 14},
hoverongaps=False,
hovertemplate=("<b>%{y}</b> · %{x}<br>Growth: %{z:+.1f}%<extra></extra>"),
xgap=2,
ygap=2,
)
)

# Layout
fig.update_layout(
title={"text": "heatmap-basic · plotly · pyplots.ai", "font": {"size": 32}, "x": 0.5, "xanchor": "center"},
xaxis={"title": {"text": "Month", "font": {"size": 24}}, "tickfont": {"size": 18}, "side": "bottom"},
title={
"text": "Monthly Sales Growth · heatmap-basic · plotly · pyplots.ai",
"font": {"size": 28},
"x": 0.5,
"xanchor": "center",
},
xaxis={"title": {"text": "Month", "font": {"size": 22}}, "tickfont": {"size": 18}, "side": "bottom"},
yaxis={
"title": {"text": "Category", "font": {"size": 24}},
"title": {"text": "Product Category", "font": {"size": 22}},
"tickfont": {"size": 18},
"autorange": "reversed", # Categories from top to bottom
"autorange": "reversed",
},
template="plotly_white",
margin={"l": 120, "r": 100, "t": 100, "b": 80},
margin={"l": 140, "r": 110, "t": 100, "b": 80},
)

# Save
Expand Down
Loading