Skip to content

Commit 76a27c3

Browse files
feat(matplotlib): implement facet-grid (#2753)
## Implementation: `facet-grid` - matplotlib Implements the **matplotlib** version of `facet-grid`. **File:** `plots/facet-grid/implementations/matplotlib.py` --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/pyplots/actions/runs/20601057118)* --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 0e9b454 commit 76a27c3

2 files changed

Lines changed: 114 additions & 0 deletions

File tree

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
""" pyplots.ai
2+
facet-grid: Faceted Grid Plot
3+
Library: matplotlib 3.10.8 | Python 3.13.11
4+
Quality: 92/100 | Created: 2025-12-30
5+
"""
6+
7+
import matplotlib.pyplot as plt
8+
import numpy as np
9+
import pandas as pd
10+
11+
12+
# Data
13+
np.random.seed(42)
14+
15+
# Create sample data with two categorical variables for faceting
16+
regions = ["North", "South", "East"]
17+
seasons = ["Spring", "Summer", "Fall", "Winter"]
18+
19+
data = []
20+
for region in regions:
21+
for season in seasons:
22+
n_points = 25
23+
# Generate temperature vs energy consumption relationship
24+
# Different patterns per region/season combination
25+
base_temp = {"North": 10, "South": 25, "East": 18}[region]
26+
season_offset = {"Spring": 5, "Summer": 15, "Fall": 0, "Winter": -10}[season]
27+
28+
temp = np.random.normal(base_temp + season_offset, 5, n_points)
29+
# Energy consumption varies with temperature
30+
energy = 100 + (temp - 15) ** 2 * 0.3 + np.random.normal(0, 10, n_points)
31+
32+
for t, e in zip(temp, energy, strict=True):
33+
data.append({"Temperature": t, "Energy": e, "Region": region, "Season": season})
34+
35+
df = pd.DataFrame(data)
36+
37+
# Plot - Faceted grid: rows = regions, columns = seasons
38+
fig, axes = plt.subplots(nrows=len(regions), ncols=len(seasons), figsize=(16, 9), sharex=True, sharey=True)
39+
40+
# Colors for regions
41+
colors = {"North": "#306998", "South": "#FFD43B", "East": "#4DAF4A"}
42+
43+
# Create scatter plots in each facet
44+
for i, region in enumerate(regions):
45+
for j, season in enumerate(seasons):
46+
ax = axes[i, j]
47+
subset = df[(df["Region"] == region) & (df["Season"] == season)]
48+
49+
ax.scatter(
50+
subset["Temperature"],
51+
subset["Energy"],
52+
s=120,
53+
alpha=0.7,
54+
color=colors[region],
55+
edgecolor="white",
56+
linewidth=0.5,
57+
)
58+
59+
# Add grid
60+
ax.grid(True, alpha=0.3, linestyle="--")
61+
62+
# Column headers (top row only)
63+
if i == 0:
64+
ax.set_title(season, fontsize=18, fontweight="bold")
65+
66+
# Row labels (rightmost column only)
67+
if j == len(seasons) - 1:
68+
ax.annotate(
69+
region, xy=(1.05, 0.5), xycoords="axes fraction", fontsize=16, fontweight="bold", va="center", ha="left"
70+
)
71+
72+
# Tick parameters
73+
ax.tick_params(axis="both", labelsize=12)
74+
75+
# Shared axis labels
76+
fig.supxlabel("Temperature (°C)", fontsize=20, y=0.02)
77+
fig.supylabel("Energy Consumption (kWh)", fontsize=20, x=0.02)
78+
79+
# Main title
80+
fig.suptitle("facet-grid · matplotlib · pyplots.ai", fontsize=24, y=0.98)
81+
82+
plt.tight_layout(rect=[0.04, 0.04, 0.95, 0.95])
83+
plt.savefig("plot.png", dpi=300, bbox_inches="tight")
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
library: matplotlib
2+
specification_id: facet-grid
3+
created: '2025-12-30T16:30:15Z'
4+
updated: '2025-12-30T16:38:05Z'
5+
generated_by: claude-opus-4-5-20251101
6+
workflow_run: 20601057118
7+
issue: 0
8+
python_version: 3.13.11
9+
library_version: 3.10.8
10+
preview_url: https://storage.googleapis.com/pyplots-images/plots/facet-grid/matplotlib/plot.png
11+
preview_thumb: https://storage.googleapis.com/pyplots-images/plots/facet-grid/matplotlib/plot_thumb.png
12+
preview_html: null
13+
quality_score: 92
14+
review:
15+
strengths:
16+
- Excellent two-way faceting implementation with clear row and column structure
17+
- Realistic temperature vs energy consumption scenario that varies meaningfully
18+
across regions and seasons
19+
- Clean KISS code structure with appropriate use of pandas for data management
20+
- Good color palette that distinguishes regions while remaining colorblind-accessible
21+
- Proper use of shared axes (sharex=True, sharey=True) for easy comparison across
22+
facets
23+
- Effective use of supxlabel/supylabel for shared axis labels
24+
- White edge on markers improves visibility when points overlap
25+
weaknesses:
26+
- Grid and legend score could be improved by adding a small legend explaining region
27+
colors
28+
- Row labels positioned with xy=(1.05, 0.5) are functional but tight_layout rect
29+
may clip them slightly
30+
- South summer temperatures reaching ~50°C are on the extreme end for realistic
31+
data

0 commit comments

Comments
 (0)