-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaltair.py
More file actions
100 lines (87 loc) · 3.16 KB
/
Copy pathaltair.py
File metadata and controls
100 lines (87 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
""" anyplot.ai
facet-grid: Faceted Grid Plot
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)
soil_types = ["Sandy", "Clay", "Loam"]
seasons = ["Spring", "Summer", "Fall"]
crop_types = ["Wheat", "Corn", "Soybean"]
n_per_group = 20
data = []
for soil in soil_types:
for season in seasons:
for crop in crop_types:
base_yield = {"Sandy": 3.5, "Clay": 4.0, "Loam": 5.0}[soil]
season_mult = {"Spring": 0.9, "Summer": 1.1, "Fall": 1.0}[season]
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)
water = np.random.normal(50 + crop_base * 5, 8, n_per_group)
for y, w in zip(yield_val, water, strict=True):
data.append(
{
"Yield (tons/ha)": max(0.5, y),
"Water Usage (mm)": max(20, w),
"Soil Type": soil,
"Season": season,
"Crop": crop,
}
)
df = pd.DataFrame(data)
# Create faceted chart with scatter plots
chart = (
alt.Chart(df)
.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=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=22, labelFontSize=18), sort=["Spring", "Summer", "Fall"]
),
row=alt.Row(
"Soil Type:N", header=alt.Header(titleFontSize=22, labelFontSize=18), sort=["Sandy", "Clay", "Loam"]
),
)
.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(f"plot-{THEME}.png", scale_factor=3.0)
chart.save(f"plot-{THEME}.html")