|
1 | | -""" pyplots.ai |
| 1 | +""" anyplot.ai |
2 | 2 | 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 |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import os |
| 8 | + |
7 | 9 | import altair as alt |
8 | 10 | import numpy as np |
9 | 11 | import pandas as pd |
10 | 12 |
|
11 | 13 |
|
| 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 | + |
12 | 23 | # Data - Crop yield across different soil types and seasons |
13 | 24 | np.random.seed(42) |
14 | 25 |
|
|
21 | 32 | for soil in soil_types: |
22 | 33 | for season in seasons: |
23 | 34 | for crop in crop_types: |
24 | | - # Base yield varies by soil type |
25 | 35 | base_yield = {"Sandy": 3.5, "Clay": 4.0, "Loam": 5.0}[soil] |
26 | | - # Season affects yield |
27 | 36 | season_mult = {"Spring": 0.9, "Summer": 1.1, "Fall": 1.0}[season] |
28 | | - # Crop type affects yield and water needs |
29 | 37 | crop_base = {"Wheat": 3.8, "Corn": 4.5, "Soybean": 3.2}[crop] |
30 | 38 |
|
31 | 39 | yield_val = np.random.normal(base_yield * season_mult + crop_base, 0.8, n_per_group) |
|
44 | 52 |
|
45 | 53 | df = pd.DataFrame(data) |
46 | 54 |
|
47 | | -# Create faceted chart with scatter plots - facet by soil and season, color by crop |
| 55 | +# Create faceted chart with scatter plots |
48 | 56 | chart = ( |
49 | 57 | alt.Chart(df) |
50 | | - .mark_circle(size=100, opacity=0.7) |
| 58 | + .mark_circle(size=150, opacity=0.7) |
51 | 59 | .encode( |
52 | 60 | x=alt.X("Water Usage (mm):Q", scale=alt.Scale(zero=False)), |
53 | 61 | 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])), |
55 | 63 | tooltip=["Yield (tons/ha)", "Water Usage (mm)", "Soil Type", "Season", "Crop"], |
56 | 64 | ) |
57 | 65 | .properties(width=320, height=260) |
58 | 66 | .facet( |
59 | 67 | 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"] |
61 | 69 | ), |
62 | 70 | 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"] |
64 | 72 | ), |
65 | 73 | ) |
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) |
70 | 96 | ) |
71 | 97 |
|
72 | 98 | # 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