{"spec_id":"andrews-curves","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\nandrews-curves: Andrews Curves for Multivariate Data\nLibrary: bokeh 3.9.0 | Python 3.13.13\nQuality: 92/100 | Updated: 2026-05-15\n\"\"\"\n\nimport os\nimport time\nfrom pathlib import Path\n\nimport numpy as np\nfrom bokeh.io import output_file, save\nfrom bokeh.models import ColumnDataSource, HoverTool, Legend\nfrom bokeh.plotting import figure\nfrom selenium import webdriver\nfrom selenium.webdriver.chrome.options import Options\nfrom sklearn.datasets import load_iris\nfrom sklearn.preprocessing import StandardScaler\n\n\n# Theme tokens\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\"\n\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\"]\n\n# Data\niris = load_iris()\nX = iris.data\ny = iris.target\nspecies_names = [\"Setosa\", \"Versicolor\", \"Virginica\"]\n\n# Normalize variables to similar scales\nscaler = StandardScaler()\nX_scaled = scaler.fit_transform(X)\n\n# Generate t values from -π to π\nt_values = np.linspace(-np.pi, np.pi, 200)\n\n# Create figure\np = figure(\n    width=4800,\n    height=2700,\n    title=\"andrews-curves · bokeh · anyplot.ai\",\n    x_axis_label=\"t (radians)\",\n    y_axis_label=\"f(t)\",\n    background_fill_color=PAGE_BG,\n    border_fill_color=PAGE_BG,\n)\n\n# Style text\np.title.text_font_size = \"28pt\"\np.title.text_color = INK\np.xaxis.axis_label_text_font_size = \"22pt\"\np.xaxis.axis_label_text_color = INK\np.yaxis.axis_label_text_font_size = \"22pt\"\np.yaxis.axis_label_text_color = INK\np.xaxis.major_label_text_font_size = \"18pt\"\np.xaxis.major_label_text_color = INK_SOFT\np.yaxis.major_label_text_font_size = \"18pt\"\np.yaxis.major_label_text_color = INK_SOFT\n\n# Style axes and grid\np.outline_line_color = INK_SOFT\np.xaxis.axis_line_color = INK_SOFT\np.yaxis.axis_line_color = INK_SOFT\np.xaxis.major_tick_line_color = INK_SOFT\np.yaxis.major_tick_line_color = INK_SOFT\np.xgrid.grid_line_color = INK\np.ygrid.grid_line_color = INK\np.xgrid.grid_line_alpha = 0.10\np.ygrid.grid_line_alpha = 0.10\n\n# Store legend items\nlegend_items = []\n\n# Plot curves for each species using vectorized Andrews curves\nfor species_idx in range(3):\n    species_mask = y == species_idx\n    X_species = X_scaled[species_mask]\n\n    first_line = None\n\n    for coeffs in X_species:\n        # Vectorized Andrews curve: f(t) = x1/sqrt(2) + x2*sin(t) + x3*cos(t) + x4*sin(2t) + ...\n        n = len(coeffs)\n        curve_values = coeffs[0] / np.sqrt(2)\n        for i in range(1, n):\n            if i % 2 == 1:\n                curve_values += coeffs[i] * np.sin((i // 2 + 1) * t_values)\n            else:\n                curve_values += coeffs[i] * np.cos((i // 2) * t_values)\n\n        source = ColumnDataSource(\n            data={\"x\": t_values, \"y\": curve_values, \"species\": [species_names[species_idx]] * len(t_values)}\n        )\n\n        line = p.line(x=\"x\", y=\"y\", source=source, line_color=IMPRINT[species_idx], line_alpha=0.4, line_width=3)\n\n        if first_line is None:\n            first_line = line\n\n    legend_items.append((species_names[species_idx], [first_line]))\n\n# Add hover tool\nhover = HoverTool(tooltips=[(\"t\", \"@x{0.00}\"), (\"f(t)\", \"@y{0.00}\"), (\"Species\", \"@species\")])\np.add_tools(hover)\n\n# Create and add legend\nlegend = Legend(items=legend_items, location=\"top_right\")\nlegend.label_text_font_size = \"18pt\"\nlegend.label_text_color = INK_SOFT\nlegend.background_fill_color = ELEVATED_BG\nlegend.background_fill_alpha = 0.95\nlegend.border_line_color = INK_SOFT\np.add_layout(legend, \"right\")\n\n# Save HTML\noutput_file(f\"plot-{THEME}.html\")\nsave(p)\n\n# Screenshot with headless Chrome via Selenium\nW, H = 4800, 2700\nopts = Options()\nfor arg in (\n    \"--headless=new\",\n    \"--no-sandbox\",\n    \"--disable-dev-shm-usage\",\n    \"--disable-gpu\",\n    f\"--window-size={W},{H}\",\n    \"--hide-scrollbars\",\n):\n    opts.add_argument(arg)\ndriver = webdriver.Chrome(options=opts)\ndriver.set_window_size(W, H)\ndriver.get(f\"file://{Path(f'plot-{THEME}.html').resolve()}\")\ntime.sleep(3)\ndriver.save_screenshot(f\"plot-{THEME}.png\")\ndriver.quit()\n"}