{"spec_id":"andrews-curves","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\nandrews-curves: Andrews Curves for Multivariate Data\nLibrary: plotly 6.7.0 | Python 3.13.13\nQuality: 85/100 | Updated: 2026-05-15\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport plotly.graph_objects as go\nfrom sklearn.datasets import load_iris\n\n\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nELEVATED_BG = \"#FFFDF6\" if THEME == \"light\" else \"#242420\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\nGRID = \"rgba(26,26,23,0.10)\" if THEME == \"light\" else \"rgba(240,239,232,0.10)\"\n\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\"]\nBRAND = IMPRINT[0]\n\n# Data\niris = load_iris()\nX = iris.data\ny = iris.target\nspecies_names = iris.target_names\n\n# Normalize\nX_normalized = (X - X.mean(axis=0)) / X.std(axis=0)\n\n# Andrews curve transformation parameter\nt = np.linspace(-np.pi, np.pi, 200)\n\n# Plot\nfig = go.Figure()\n\n# Plot curves for each sample, colored by species\nfor species_idx in range(3):\n    species_mask = y == species_idx\n    X_species = X_normalized[species_mask]\n\n    for i, x in enumerate(X_species):\n        # Inline Andrews curve transformation\n        n_features = len(x)\n        curve = np.ones_like(t) * x[0] / np.sqrt(2)\n        for j in range(1, n_features):\n            freq = (j + 1) // 2\n            if j % 2 == 1:\n                curve += x[j] * np.sin(freq * t)\n            else:\n                curve += x[j] * np.cos(freq * t)\n\n        fig.add_trace(\n            go.Scatter(\n                x=t,\n                y=curve,\n                mode=\"lines\",\n                line=dict(color=IMPRINT[species_idx], width=2),\n                opacity=0.4,\n                name=species_names[species_idx],\n                legendgroup=species_names[species_idx],\n                showlegend=(i == 0),\n                hovertemplate=f\"{species_names[species_idx]}<br>t: %{{x:.2f}}<br>f(t): %{{y:.2f}}<extra></extra>\",\n            )\n        )\n\n# Layout\nfig.update_layout(\n    title=dict(text=\"andrews-curves · plotly · anyplot.ai\", font=dict(size=28, color=INK), x=0.5, xanchor=\"center\"),\n    xaxis=dict(\n        title=dict(text=\"Parameter t (radians)\", font=dict(size=22, color=INK)),\n        tickfont=dict(size=18, color=INK_SOFT),\n        gridcolor=GRID,\n        gridwidth=0.5,\n        zeroline=True,\n        zerolinecolor=GRID,\n        zerolinewidth=1,\n        linecolor=INK_SOFT,\n        range=[-np.pi, np.pi],\n        tickvals=[-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi],\n        ticktext=[\"-π\", \"-π/2\", \"0\", \"π/2\", \"π\"],\n    ),\n    yaxis=dict(\n        title=dict(text=\"f(t) (normalized units)\", font=dict(size=22, color=INK)),\n        tickfont=dict(size=18, color=INK_SOFT),\n        gridcolor=GRID,\n        gridwidth=0.5,\n        zeroline=True,\n        zerolinecolor=GRID,\n        zerolinewidth=1,\n        linecolor=INK_SOFT,\n    ),\n    legend=dict(\n        font=dict(size=16, color=INK_SOFT),\n        x=0.98,\n        y=0.98,\n        xanchor=\"right\",\n        yanchor=\"top\",\n        bgcolor=ELEVATED_BG,\n        bordercolor=INK_SOFT,\n        borderwidth=1,\n    ),\n    margin=dict(l=120, r=80, t=100, b=100),\n    plot_bgcolor=PAGE_BG,\n    paper_bgcolor=PAGE_BG,\n    font=dict(color=INK),\n)\n\n# Save\nfig.write_image(f\"plot-{THEME}.png\", width=1600, height=900, scale=3)\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}