{"spec_id":"scatter-matrix","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\nscatter-matrix: Scatter Plot Matrix\nLibrary: bokeh 3.9.0 | Python 3.13.13\nQuality: 91/100 | Updated: 2026-05-09\n\"\"\"\n\nimport os\nimport time\nfrom pathlib import Path\n\nimport numpy as np\nfrom bokeh.io import output_file, save\nfrom bokeh.layouts import gridplot\nfrom bokeh.models import ColumnDataSource, Title\nfrom bokeh.plotting import figure\nfrom bokeh.transform import factor_cmap\nfrom selenium import webdriver\nfrom selenium.webdriver.chrome.options import Options\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\n# Okabe-Ito palette (first series always #009E73)\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\"]\nspecies_list = [\"setosa\", \"versicolor\", \"virginica\"]\n\n# Data - Iris-like dataset with 4 variables and species groups\nnp.random.seed(42)\n\nspecies = np.repeat(species_list, 50)\n\nsepal_length = np.concatenate(\n    [np.random.normal(5.0, 0.35, 50), np.random.normal(5.9, 0.52, 50), np.random.normal(6.6, 0.64, 50)]\n)\n\nsepal_width = np.concatenate(\n    [np.random.normal(3.4, 0.38, 50), np.random.normal(2.8, 0.31, 50), np.random.normal(3.0, 0.32, 50)]\n)\n\npetal_length = np.concatenate(\n    [np.random.normal(1.5, 0.17, 50), np.random.normal(4.3, 0.47, 50), np.random.normal(5.6, 0.55, 50)]\n)\n\npetal_width = np.concatenate(\n    [np.random.normal(0.2, 0.10, 50), np.random.normal(1.3, 0.20, 50), np.random.normal(2.0, 0.27, 50)]\n)\n\nvariables = [sepal_length, sepal_width, petal_length, petal_width]\nvar_names = [\"Sepal Length\", \"Sepal Width\", \"Petal Length\", \"Petal Width\"]\nn_vars = len(variables)\n\n# Create scatter matrix grid\ncell_size = 900\ngrid = []\n\nfor i in range(n_vars):\n    row = []\n    for j in range(n_vars):\n        x_label = var_names[j] if i == n_vars - 1 else \"\"\n        y_label = var_names[i] if j == 0 else \"\"\n\n        if i == j:\n            # Diagonal: Histogram\n            p = figure(width=cell_size, height=cell_size, x_axis_label=x_label, y_axis_label=y_label, tools=\"\")\n\n            hist_data = variables[i]\n            bins = np.linspace(hist_data.min() - 0.1, hist_data.max() + 0.1, 20)\n\n            for k, sp in enumerate(species_list):\n                mask = species == sp\n                hist, edges = np.histogram(hist_data[mask], bins=bins)\n                source = ColumnDataSource(data={\"top\": hist, \"left\": edges[:-1], \"right\": edges[1:]})\n                p.quad(\n                    top=\"top\",\n                    bottom=0,\n                    left=\"left\",\n                    right=\"right\",\n                    source=source,\n                    fill_color=IMPRINT[k],\n                    line_color=PAGE_BG,\n                    line_width=1.5,\n                    alpha=0.7,\n                    legend_label=sp.capitalize(),\n                )\n\n            p.y_range.start = 0\n            if i == 0:\n                p.legend.location = \"top_right\"\n                p.legend.label_text_font_size = \"16pt\"\n                p.legend.glyph_height = 20\n                p.legend.glyph_width = 20\n                p.legend.spacing = 8\n            else:\n                p.legend.visible = False\n\n        else:\n            # Off-diagonal: Scatter plot with refined styling\n            source = ColumnDataSource(data={\"x\": variables[j], \"y\": variables[i], \"species\": species})\n\n            p = figure(width=cell_size, height=cell_size, x_axis_label=x_label, y_axis_label=y_label, tools=\"\")\n\n            p.scatter(\n                x=\"x\",\n                y=\"y\",\n                source=source,\n                size=14,\n                alpha=0.65,\n                fill_color=factor_cmap(\"species\", IMPRINT, species_list),\n                line_color=\"white\" if THEME == \"light\" else \"#2A2A27\",\n                line_width=0.8,\n            )\n\n        # Style axes — minimal, refined aesthetic\n        p.background_fill_color = PAGE_BG\n        p.border_fill_color = PAGE_BG\n        p.outline_line_width = 0  # Remove spines for cleaner look\n\n        p.title.text_color = INK\n        p.xaxis.axis_label_text_color = INK\n        p.yaxis.axis_label_text_color = INK\n        p.xaxis.major_label_text_color = INK_SOFT\n        p.yaxis.major_label_text_color = INK_SOFT\n        p.xaxis.axis_line_color = INK_SOFT\n        p.yaxis.axis_line_color = INK_SOFT\n        p.xaxis.major_tick_line_color = INK_SOFT\n        p.yaxis.major_tick_line_color = INK_SOFT\n        p.xaxis.major_tick_line_width = 1\n        p.yaxis.major_tick_line_width = 1\n\n        p.xaxis.axis_label_text_font_size = \"20pt\"\n        p.yaxis.axis_label_text_font_size = \"20pt\"\n        p.xaxis.major_label_text_font_size = \"16pt\"\n        p.yaxis.major_label_text_font_size = \"16pt\"\n\n        p.xgrid.grid_line_color = INK\n        p.ygrid.grid_line_color = INK\n        p.xgrid.grid_line_alpha = 0.08\n        p.ygrid.grid_line_alpha = 0.08\n\n        if p.legend:\n            p.legend.background_fill_color = ELEVATED_BG\n            p.legend.border_line_color = INK_SOFT\n            p.legend.label_text_color = INK_SOFT\n            p.legend.border_line_width = 1\n            p.legend.label_text_font_size = \"16pt\"\n\n        row.append(p)\n\n    grid.append(row)\n\n# Add title\ngrid[0][0].add_layout(\n    Title(text=\"scatter-matrix · bokeh · anyplot.ai\", text_font_size=\"28pt\", align=\"left\", text_color=INK), \"above\"\n)\n\n# Create grid layout\nlayout = gridplot(grid, toolbar_location=None, merge_tools=False)\n\n# Save HTML\noutput_file(f\"plot-{THEME}.html\")\nsave(layout)\n\n# Screenshot with headless Chrome\nW, H = 3600, 3600\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"}