|
1 | | -""" pyplots.ai |
| 1 | +""" anyplot.ai |
2 | 2 | andrews-curves: Andrews Curves for Multivariate Data |
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: 92/100 | Updated: 2026-05-15 |
5 | 5 | """ |
6 | 6 |
|
7 | | -import altair as alt |
8 | | -import numpy as np |
9 | | -import pandas as pd |
10 | | -from sklearn.datasets import load_iris |
11 | | -from sklearn.preprocessing import StandardScaler |
| 7 | +import os |
| 8 | +import sys |
12 | 9 |
|
13 | 10 |
|
14 | | -# Set random seed for reproducibility |
15 | | -np.random.seed(42) |
| 11 | +# Remove script directory from sys.path to avoid importing local altair.py |
| 12 | +script_dir = os.path.dirname(os.path.abspath(__file__)) |
| 13 | +if script_dir in sys.path: |
| 14 | + sys.path.remove(script_dir) |
| 15 | + |
| 16 | +import altair as alt # noqa: E402 |
| 17 | +import numpy as np # noqa: E402 |
| 18 | +import pandas as pd # noqa: E402 |
| 19 | +from sklearn.datasets import load_iris # noqa: E402 |
| 20 | +from sklearn.preprocessing import StandardScaler # noqa: E402 |
| 21 | + |
| 22 | + |
| 23 | +# Theme tokens |
| 24 | +THEME = os.getenv("ANYPLOT_THEME", "light") |
| 25 | +PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17" |
| 26 | +ELEVATED_BG = "#FFFDF6" if THEME == "light" else "#242420" |
| 27 | +INK = "#1A1A17" if THEME == "light" else "#F0EFE8" |
| 28 | +INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0" |
| 29 | + |
| 30 | +# Okabe-Ito palette |
| 31 | +OKABE_ITO = ["#009E73", "#D55E00", "#0072B2"] |
16 | 32 |
|
17 | 33 | # Load and prepare data |
| 34 | +np.random.seed(42) |
18 | 35 | iris = load_iris() |
19 | 36 | X = iris.data |
20 | 37 | y = iris.target |
|
50 | 67 |
|
51 | 68 | df = pd.DataFrame(curves_data) |
52 | 69 |
|
53 | | -# Define colors for species |
54 | | -species_colors = ["#306998", "#FFD43B", "#6B8E23"] |
55 | | - |
56 | 70 | # Create chart |
57 | 71 | chart = ( |
58 | 72 | alt.Chart(df) |
59 | | - .mark_line(opacity=0.4, strokeWidth=2) |
| 73 | + .mark_line(opacity=0.5, size=2) |
60 | 74 | .encode( |
61 | | - x=alt.X("t:Q", title="t (radians)", axis=alt.Axis(labelFontSize=18, titleFontSize=22)), |
62 | | - y=alt.Y("value:Q", title="Andrews Curve Value", axis=alt.Axis(labelFontSize=18, titleFontSize=22)), |
| 75 | + x=alt.X( |
| 76 | + "t:Q", |
| 77 | + title="t (radians)", |
| 78 | + axis=alt.Axis(labelFontSize=18, titleFontSize=22, labelColor=INK_SOFT, titleColor=INK), |
| 79 | + ), |
| 80 | + y=alt.Y( |
| 81 | + "value:Q", |
| 82 | + title="Andrews Curve Value", |
| 83 | + axis=alt.Axis(labelFontSize=18, titleFontSize=22, labelColor=INK_SOFT, titleColor=INK), |
| 84 | + ), |
63 | 85 | color=alt.Color( |
64 | 86 | "species:N", |
65 | 87 | title="Species", |
66 | | - scale=alt.Scale(domain=["Setosa", "Versicolor", "Virginica"], range=species_colors), |
67 | | - legend=alt.Legend(titleFontSize=22, labelFontSize=20), |
| 88 | + scale=alt.Scale(domain=species_names, range=OKABE_ITO), |
| 89 | + legend=alt.Legend( |
| 90 | + titleFontSize=18, |
| 91 | + labelFontSize=16, |
| 92 | + titleColor=INK, |
| 93 | + labelColor=INK_SOFT, |
| 94 | + fillColor=ELEVATED_BG, |
| 95 | + strokeColor=INK_SOFT, |
| 96 | + ), |
68 | 97 | ), |
69 | 98 | detail="observation:N", |
| 99 | + tooltip=["species:N", "observation:N"], |
70 | 100 | ) |
71 | 101 | .properties( |
72 | 102 | width=1600, |
73 | 103 | height=900, |
74 | | - title=alt.Title( |
75 | | - "Iris Classification · andrews-curves · altair · pyplots.ai", |
76 | | - fontSize=28, |
77 | | - subtitle="Fourier series transformation of multivariate data for visual pattern comparison", |
78 | | - subtitleFontSize=18, |
79 | | - ), |
| 104 | + background=PAGE_BG, |
| 105 | + title=alt.Title("andrews-curves · altair · anyplot.ai", fontSize=28, color=INK), |
| 106 | + ) |
| 107 | + .configure_axis(domainColor=INK_SOFT, tickColor=INK_SOFT, gridColor=INK, gridOpacity=0.10) |
| 108 | + .configure_view(fill=PAGE_BG, stroke=None) |
| 109 | + .configure_title(color=INK) |
| 110 | + .configure_legend( |
| 111 | + titleFontSize=18, |
| 112 | + labelFontSize=16, |
| 113 | + titleColor=INK, |
| 114 | + labelColor=INK_SOFT, |
| 115 | + fillColor=ELEVATED_BG, |
| 116 | + strokeColor=INK_SOFT, |
80 | 117 | ) |
81 | | - .configure_axis(labelFontSize=18, titleFontSize=22) |
82 | | - .configure_view(strokeWidth=0) |
83 | | - .configure_title(fontSize=28) |
84 | 118 | ) |
85 | 119 |
|
86 | 120 | # Save as PNG and HTML |
87 | | -chart.save("plot.png", scale_factor=3.0) |
88 | | -chart.save("plot.html") |
| 121 | +chart.save(f"plot-{THEME}.png", scale_factor=3.0) |
| 122 | +chart.save(f"plot-{THEME}.html") |
0 commit comments