{"spec_id":"scatter-marginal","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\nscatter-marginal: Scatter Plot with Marginal Distributions\nLibrary: bokeh 3.9.0 | Python 3.13.13\nQuality: 90/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 column, row\nfrom bokeh.models import ColumnDataSource\nfrom bokeh.plotting import figure\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\"\nBRAND = \"#009E73\"  # Okabe-Ito position 1\n\n# Data - bivariate normal with correlation\nnp.random.seed(42)\nn_points = 200\nx = np.random.randn(n_points) * 15 + 50\ny = x * 0.6 + np.random.randn(n_points) * 10 + 20\n\nsource = ColumnDataSource(data={\"x\": x, \"y\": y})\n\n# Calculate dimensions for 4800x2700 total with marginal plots\nmain_width = 3800\nmain_height = 2100\nmarginal_width = 3800\nmarginal_height = 550\nside_marginal_width = 950\nside_marginal_height = 2100\n\n# Histogram bins\nn_bins = 30\nx_hist, x_edges = np.histogram(x, bins=n_bins)\ny_hist, y_edges = np.histogram(y, bins=n_bins)\n\n# Main scatter plot\np_scatter = figure(\n    width=main_width,\n    height=main_height,\n    x_axis_label=\"Height (cm)\",\n    y_axis_label=\"Weight (kg)\",\n    title=\"scatter-marginal · bokeh · anyplot.ai\",\n)\n\np_scatter.scatter(x=\"x\", y=\"y\", source=source, size=20, color=BRAND, alpha=0.65, line_color=None)\n\n# Style main scatter\np_scatter.title.text_font_size = \"28pt\"\np_scatter.title.text_color = INK\np_scatter.xaxis.axis_label_text_font_size = \"22pt\"\np_scatter.yaxis.axis_label_text_font_size = \"22pt\"\np_scatter.xaxis.axis_label_text_color = INK\np_scatter.yaxis.axis_label_text_color = INK\np_scatter.xaxis.major_label_text_font_size = \"18pt\"\np_scatter.yaxis.major_label_text_font_size = \"18pt\"\np_scatter.xaxis.major_label_text_color = INK_SOFT\np_scatter.yaxis.major_label_text_color = INK_SOFT\np_scatter.xaxis.axis_line_color = INK_SOFT\np_scatter.yaxis.axis_line_color = INK_SOFT\np_scatter.xaxis.major_tick_line_color = INK_SOFT\np_scatter.yaxis.major_tick_line_color = INK_SOFT\np_scatter.background_fill_color = PAGE_BG\np_scatter.border_fill_color = PAGE_BG\np_scatter.outline_line_color = INK_SOFT\np_scatter.grid.grid_line_color = INK\np_scatter.grid.grid_line_alpha = 0.10\np_scatter.toolbar_location = None\n\n# Top marginal histogram (X distribution)\np_top = figure(width=marginal_width, height=marginal_height, x_range=p_scatter.x_range, title=None)\np_top.quad(top=x_hist, bottom=0, left=x_edges[:-1], right=x_edges[1:], fill_color=BRAND, line_color=None, alpha=0.6)\np_top.xaxis.visible = False\np_top.yaxis.axis_label = \"Count\"\np_top.yaxis.axis_label_text_font_size = \"18pt\"\np_top.yaxis.axis_label_text_color = INK\np_top.yaxis.major_label_text_font_size = \"14pt\"\np_top.yaxis.major_label_text_color = INK_SOFT\np_top.yaxis.axis_line_color = INK_SOFT\np_top.yaxis.major_tick_line_color = INK_SOFT\np_top.background_fill_color = PAGE_BG\np_top.border_fill_color = PAGE_BG\np_top.outline_line_color = INK_SOFT\np_top.grid.grid_line_color = INK\np_top.grid.grid_line_alpha = 0.10\np_top.min_border_bottom = 0\np_top.min_border_left = p_scatter.min_border_left\np_top.toolbar_location = None\n\n# Right marginal histogram (Y distribution)\np_right = figure(width=side_marginal_width, height=main_height, y_range=p_scatter.y_range, title=None)\np_right.quad(top=y_edges[1:], bottom=y_edges[:-1], left=0, right=y_hist, fill_color=BRAND, line_color=None, alpha=0.6)\np_right.yaxis.visible = False\np_right.xaxis.axis_label = \"Count\"\np_right.xaxis.axis_label_text_font_size = \"18pt\"\np_right.xaxis.axis_label_text_color = INK\np_right.xaxis.major_label_text_font_size = \"14pt\"\np_right.xaxis.major_label_text_color = INK_SOFT\np_right.xaxis.axis_line_color = INK_SOFT\np_right.xaxis.major_tick_line_color = INK_SOFT\np_right.background_fill_color = PAGE_BG\np_right.border_fill_color = PAGE_BG\np_right.outline_line_color = INK_SOFT\np_right.grid.grid_line_color = INK\np_right.grid.grid_line_alpha = 0.10\np_right.min_border_left = 0\np_right.min_border_bottom = p_scatter.min_border_bottom\np_right.toolbar_location = None\n\n# Empty corner placeholder\np_corner = figure(width=side_marginal_width, height=marginal_height, toolbar_location=None)\np_corner.outline_line_color = None\np_corner.xaxis.visible = False\np_corner.yaxis.visible = False\np_corner.grid.visible = False\np_corner.background_fill_color = PAGE_BG\np_corner.border_fill_color = PAGE_BG\n\n# Layout\nlayout = column(row(p_top, p_corner), row(p_scatter, p_right))\n\n# Save HTML\noutput_file(f\"plot-{THEME}.html\")\nsave(layout)\n\n# Screenshot with 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)\n\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"}