Skip to content

Commit 05c1da0

Browse files
feat(plotly): implement facet-grid (#2752)
## Implementation: `facet-grid` - plotly Implements the **plotly** version of `facet-grid`. **File:** `plots/facet-grid/implementations/plotly.py` --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20601057130)* --------- 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 981d533 commit 05c1da0

2 files changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
""" pyplots.ai
2+
facet-grid: Faceted Grid Plot
3+
Library: plotly 6.5.0 | Python 3.13.11
4+
Quality: 91/100 | Created: 2025-12-30
5+
"""
6+
7+
import numpy as np
8+
import pandas as pd
9+
import plotly.express as px
10+
11+
12+
# Data - Create realistic dataset for faceted visualization
13+
np.random.seed(42)
14+
15+
# Product performance data across regions and quarters
16+
regions = ["North", "South", "East", "West"]
17+
quarters = ["Q1", "Q2", "Q3", "Q4"]
18+
19+
data = []
20+
for region in regions:
21+
for quarter in quarters:
22+
# Generate 15 data points per region-quarter combination
23+
n = 15
24+
# Base values vary by region and quarter
25+
base_sales = {"North": 80, "South": 60, "East": 70, "West": 75}
26+
quarter_effect = {"Q1": 0, "Q2": 10, "Q3": 5, "Q4": 15}
27+
28+
marketing_spend = np.random.uniform(10, 50, n)
29+
sales = (
30+
base_sales[region]
31+
+ quarter_effect[quarter]
32+
+ marketing_spend * (0.8 + np.random.uniform(0, 0.4, n))
33+
+ np.random.normal(0, 5, n)
34+
)
35+
36+
for i in range(n):
37+
data.append(
38+
{
39+
"Marketing Spend ($K)": marketing_spend[i],
40+
"Sales ($K)": sales[i],
41+
"Region": region,
42+
"Quarter": quarter,
43+
}
44+
)
45+
46+
df = pd.DataFrame(data)
47+
48+
# Create faceted scatter plot
49+
# Using single color since Region is already encoded by facet rows
50+
fig = px.scatter(df, x="Marketing Spend ($K)", y="Sales ($K)", facet_row="Region", facet_col="Quarter")
51+
52+
# Update layout for large canvas
53+
fig.update_layout(
54+
title=dict(text="facet-grid · plotly · pyplots.ai", font=dict(size=32), x=0.5, xanchor="center"),
55+
font=dict(size=16),
56+
template="plotly_white",
57+
showlegend=False,
58+
margin=dict(l=80, r=80, t=100, b=80),
59+
)
60+
61+
# Update axes for all facets
62+
fig.update_xaxes(title_font=dict(size=20), tickfont=dict(size=16), gridcolor="rgba(0,0,0,0.1)", gridwidth=1)
63+
64+
fig.update_yaxes(title_font=dict(size=20), tickfont=dict(size=16), gridcolor="rgba(0,0,0,0.1)", gridwidth=1)
65+
66+
# Update facet labels (annotations)
67+
fig.for_each_annotation(lambda a: a.update(font=dict(size=18)))
68+
69+
# Update markers with consistent color for visibility
70+
fig.update_traces(marker=dict(size=12, opacity=0.8, color="#306998", line=dict(width=1, color="white")))
71+
72+
# Save as PNG and HTML
73+
fig.write_image("plot.png", width=1600, height=900, scale=3)
74+
fig.write_html("plot.html", include_plotlyjs="cdn")
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
library: plotly
2+
specification_id: facet-grid
3+
created: '2025-12-30T16:30:15Z'
4+
updated: '2025-12-30T16:47:28Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20601057130
7+
issue: 0
8+
python_version: 3.13.11
9+
library_version: 6.5.0
10+
preview_url: https://storage.googleapis.com/pyplots-images/plots/facet-grid/plotly/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/facet-grid/plotly/plot_thumb.png
12+
preview_html: https://storage.googleapis.com/pyplots-images/plots/facet-grid/plotly/plot.html
13+
quality_score: 91
14+
review:
15+
strengths:
16+
- Clean implementation using Plotly Express native faceting capability (facet_row,
17+
facet_col)
18+
- Excellent data generation with realistic business scenario (marketing spend vs
19+
sales by region and quarter)
20+
- Good visual hierarchy with clear title, readable axis labels with units, and subtle
21+
grid
22+
- Produces both PNG and HTML output leveraging Plotly interactivity
23+
- Proper use of template=plotly_white for clean appearance
24+
weaknesses:
25+
- Legend showing regions is redundant since region is already encoded by facet rows
26+
- Markers could be slightly larger (15-18 instead of 12) for better visibility with
27+
~15 points per facet
28+
- Row facet labels are displayed vertically which reduces readability compared to
29+
horizontal text

0 commit comments

Comments
 (0)