{"spec_id":"andrews-curves","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nandrews-curves: Andrews Curves for Multivariate Data\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-05-15\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom sklearn.datasets import load_iris\nfrom sklearn.preprocessing import StandardScaler\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\"\nBRAND = \"#009E73\"\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\"]\n\n# Data\nnp.random.seed(42)\niris = load_iris()\nX = iris.data\ny = iris.target\nspecies_names = [\"Setosa\", \"Versicolor\", \"Virginica\"]\n\n# Normalize data to prevent dominant variables\nscaler = StandardScaler()\nX_scaled = scaler.fit_transform(X)\n\n# Generate t values from -π to π\nt = np.linspace(-np.pi, np.pi, 200)\n\n# Build Andrews curve transformation matrix\n# f(t) = x1/sqrt(2) + x2*sin(t) + x3*cos(t) + x4*sin(2t) + ...\nn_features = X_scaled.shape[1]\nbasis = np.zeros((len(t), n_features))\nbasis[:, 0] = 1 / np.sqrt(2)\nfor i in range(1, n_features):\n    freq = (i + 1) // 2\n    if i % 2 == 1:\n        basis[:, i] = np.sin(freq * t)\n    else:\n        basis[:, i] = np.cos(freq * t)\n\n# Compute all Andrews curves: each row of X_scaled dot basis.T gives one curve\ncurves = X_scaled @ basis.T  # shape: (150, 200)\n\n# Plot\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Plot Andrews curves for each observation\nfor i in range(len(curves)):\n    ax.plot(t, curves[i], color=IMPRINT[y[i]], alpha=0.4, linewidth=2.5)\n\n# Create legend with sample lines\nfor idx, species in enumerate(species_names):\n    ax.plot([], [], color=IMPRINT[idx], linewidth=3, label=species, alpha=0.4)\n\n# Style\nax.set_xlabel(\"t (radians)\", fontsize=20, color=INK)\nax.set_ylabel(\"f(t)\", fontsize=20, color=INK)\nax.set_title(\"andrews-curves · matplotlib · anyplot.ai\", fontsize=24, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=16, colors=INK_SOFT)\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nfor s in (\"left\", \"bottom\"):\n    ax.spines[s].set_color(INK_SOFT)\nax.yaxis.grid(True, alpha=0.15, linewidth=0.8, color=INK)\n\n# Legend styling\nleg = ax.legend(fontsize=16, loc=\"upper right\")\nif leg:\n    leg.get_frame().set_facecolor(ELEVATED_BG)\n    leg.get_frame().set_edgecolor(INK_SOFT)\n    leg.get_frame().set_linewidth(0.8)\n    plt.setp(leg.get_texts(), color=INK_SOFT)\n\n# Set x-axis ticks at meaningful positions\nax.set_xticks([-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi])\nax.set_xticklabels([\"-π\", \"-π/2\", \"0\", \"π/2\", \"π\"], fontsize=16)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}