Skip to content

Commit d91c7ec

Browse files
feat(altair): implement facet-grid (#6518)
## Implementation: `facet-grid` - python/altair Implements the **python/altair** version of `facet-grid`. **File:** `plots/facet-grid/implementations/python/altair.py` **Parent Issue:** #2696 --- :robot: *[impl-generate workflow](https://github.com/MarkusNeusinger/anyplot/actions/runs/25776543142)* --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Markus Neusinger <2921697+MarkusNeusinger@users.noreply.github.com>
1 parent e50192d commit d91c7ec

2 files changed

Lines changed: 182 additions & 148 deletions

File tree

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
1-
""" pyplots.ai
1+
""" anyplot.ai
22
facet-grid: Faceted Grid Plot
3-
Library: altair 6.0.0 | Python 3.13.11
4-
Quality: 91/100 | Created: 2025-12-30
3+
Library: altair 6.1.0 | Python 3.13.13
4+
Quality: 91/100 | Updated: 2026-05-13
55
"""
66

7+
import os
8+
79
import altair as alt
810
import numpy as np
911
import pandas as pd
1012

1113

14+
# Theme tokens
15+
THEME = os.getenv("ANYPLOT_THEME", "light")
16+
PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17"
17+
ELEVATED_BG = "#FFFDF6" if THEME == "light" else "#242420"
18+
INK = "#1A1A17" if THEME == "light" else "#F0EFE8"
19+
INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0"
20+
21+
OKABE_ITO = ["#009E73", "#D55E00", "#0072B2", "#CC79A7", "#E69F00", "#56B4E9", "#F0E442"]
22+
1223
# Data - Crop yield across different soil types and seasons
1324
np.random.seed(42)
1425

@@ -21,11 +32,8 @@
2132
for soil in soil_types:
2233
for season in seasons:
2334
for crop in crop_types:
24-
# Base yield varies by soil type
2535
base_yield = {"Sandy": 3.5, "Clay": 4.0, "Loam": 5.0}[soil]
26-
# Season affects yield
2736
season_mult = {"Spring": 0.9, "Summer": 1.1, "Fall": 1.0}[season]
28-
# Crop type affects yield and water needs
2937
crop_base = {"Wheat": 3.8, "Corn": 4.5, "Soybean": 3.2}[crop]
3038

3139
yield_val = np.random.normal(base_yield * season_mult + crop_base, 0.8, n_per_group)
@@ -44,31 +52,49 @@
4452

4553
df = pd.DataFrame(data)
4654

47-
# Create faceted chart with scatter plots - facet by soil and season, color by crop
55+
# Create faceted chart with scatter plots
4856
chart = (
4957
alt.Chart(df)
50-
.mark_circle(size=100, opacity=0.7)
58+
.mark_circle(size=150, opacity=0.7)
5159
.encode(
5260
x=alt.X("Water Usage (mm):Q", scale=alt.Scale(zero=False)),
5361
y=alt.Y("Yield (tons/ha):Q", scale=alt.Scale(zero=False)),
54-
color=alt.Color("Crop:N", scale=alt.Scale(domain=crop_types, range=["#306998", "#FFD43B", "#4CAF50"])),
62+
color=alt.Color("Crop:N", scale=alt.Scale(domain=crop_types, range=OKABE_ITO[:3])),
5563
tooltip=["Yield (tons/ha)", "Water Usage (mm)", "Soil Type", "Season", "Crop"],
5664
)
5765
.properties(width=320, height=260)
5866
.facet(
5967
column=alt.Column(
60-
"Season:N", header=alt.Header(titleFontSize=20, labelFontSize=16), sort=["Spring", "Summer", "Fall"]
68+
"Season:N", header=alt.Header(titleFontSize=22, labelFontSize=18), sort=["Spring", "Summer", "Fall"]
6169
),
6270
row=alt.Row(
63-
"Soil Type:N", header=alt.Header(titleFontSize=20, labelFontSize=16), sort=["Sandy", "Clay", "Loam"]
71+
"Soil Type:N", header=alt.Header(titleFontSize=22, labelFontSize=18), sort=["Sandy", "Clay", "Loam"]
6472
),
6573
)
66-
.configure_axis(labelFontSize=14, titleFontSize=16)
67-
.configure_legend(titleFontSize=18, labelFontSize=16, symbolSize=180)
68-
.configure_title(fontSize=24)
69-
.properties(title="facet-grid · altair · pyplots.ai")
74+
.configure_axis(
75+
labelFontSize=16,
76+
titleFontSize=20,
77+
domainColor=INK_SOFT,
78+
tickColor=INK_SOFT,
79+
gridColor=INK,
80+
gridOpacity=0.10,
81+
labelColor=INK_SOFT,
82+
titleColor=INK,
83+
)
84+
.configure_legend(
85+
titleFontSize=18,
86+
labelFontSize=16,
87+
symbolSize=180,
88+
fillColor=ELEVATED_BG,
89+
strokeColor=INK_SOFT,
90+
labelColor=INK_SOFT,
91+
titleColor=INK,
92+
)
93+
.configure_title(fontSize=28, color=INK)
94+
.configure_view(fill=PAGE_BG, stroke=INK_SOFT)
95+
.properties(title="facet-grid · altair · anyplot.ai", background=PAGE_BG)
7096
)
7197

7298
# Save as PNG and HTML
73-
chart.save("plot.png", scale_factor=3.0)
74-
chart.save("plot.html")
99+
chart.save(f"plot-{THEME}.png", scale_factor=3.0)
100+
chart.save(f"plot-{THEME}.html")

0 commit comments

Comments
 (0)