Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 43 additions & 17 deletions plots/facet-grid/implementations/python/altair.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
""" pyplots.ai
""" anyplot.ai
facet-grid: Faceted Grid Plot
Library: altair 6.0.0 | Python 3.13.11
Quality: 91/100 | Created: 2025-12-30
Library: altair 6.1.0 | Python 3.13.13
Quality: 91/100 | Updated: 2026-05-13
"""

import os

import altair as alt
import numpy as np
import pandas as pd


# Theme tokens
THEME = os.getenv("ANYPLOT_THEME", "light")
PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17"
ELEVATED_BG = "#FFFDF6" if THEME == "light" else "#242420"
INK = "#1A1A17" if THEME == "light" else "#F0EFE8"
INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0"

OKABE_ITO = ["#009E73", "#D55E00", "#0072B2", "#CC79A7", "#E69F00", "#56B4E9", "#F0E442"]

# Data - Crop yield across different soil types and seasons
np.random.seed(42)

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

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

df = pd.DataFrame(data)

# Create faceted chart with scatter plots - facet by soil and season, color by crop
# Create faceted chart with scatter plots
chart = (
alt.Chart(df)
.mark_circle(size=100, opacity=0.7)
.mark_circle(size=150, opacity=0.7)
.encode(
x=alt.X("Water Usage (mm):Q", scale=alt.Scale(zero=False)),
y=alt.Y("Yield (tons/ha):Q", scale=alt.Scale(zero=False)),
color=alt.Color("Crop:N", scale=alt.Scale(domain=crop_types, range=["#306998", "#FFD43B", "#4CAF50"])),
color=alt.Color("Crop:N", scale=alt.Scale(domain=crop_types, range=OKABE_ITO[:3])),
tooltip=["Yield (tons/ha)", "Water Usage (mm)", "Soil Type", "Season", "Crop"],
)
.properties(width=320, height=260)
.facet(
column=alt.Column(
"Season:N", header=alt.Header(titleFontSize=20, labelFontSize=16), sort=["Spring", "Summer", "Fall"]
"Season:N", header=alt.Header(titleFontSize=22, labelFontSize=18), sort=["Spring", "Summer", "Fall"]
),
row=alt.Row(
"Soil Type:N", header=alt.Header(titleFontSize=20, labelFontSize=16), sort=["Sandy", "Clay", "Loam"]
"Soil Type:N", header=alt.Header(titleFontSize=22, labelFontSize=18), sort=["Sandy", "Clay", "Loam"]
),
)
.configure_axis(labelFontSize=14, titleFontSize=16)
.configure_legend(titleFontSize=18, labelFontSize=16, symbolSize=180)
.configure_title(fontSize=24)
.properties(title="facet-grid · altair · pyplots.ai")
.configure_axis(
labelFontSize=16,
titleFontSize=20,
domainColor=INK_SOFT,
tickColor=INK_SOFT,
gridColor=INK,
gridOpacity=0.10,
labelColor=INK_SOFT,
titleColor=INK,
)
.configure_legend(
titleFontSize=18,
labelFontSize=16,
symbolSize=180,
fillColor=ELEVATED_BG,
strokeColor=INK_SOFT,
labelColor=INK_SOFT,
titleColor=INK,
)
.configure_title(fontSize=28, color=INK)
.configure_view(fill=PAGE_BG, stroke=INK_SOFT)
.properties(title="facet-grid · altair · anyplot.ai", background=PAGE_BG)
)

# Save as PNG and HTML
chart.save("plot.png", scale_factor=3.0)
chart.save("plot.html")
chart.save(f"plot-{THEME}.png", scale_factor=3.0)
chart.save(f"plot-{THEME}.html")
Loading
Loading