{"spec_id":"scatter-marginal","library":"letsplot","language":"python","code":"\"\"\" anyplot.ai\nscatter-marginal: Scatter Plot with Marginal Distributions\nLibrary: letsplot 4.9.0 | Python 3.13.13\nQuality: 95/100 | Updated: 2026-05-09\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport pandas as pd\nfrom lets_plot import *\nfrom lets_plot.export import ggsave as export_ggsave\n\n\nLetsPlot.setup_html()\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\nBRAND = \"#009E73\"  # Okabe-Ito position 1\nACCENT = \"#C475FD\"  # Okabe-Ito position 2\n\n# Data - bivariate data with correlation\nnp.random.seed(42)\nn = 200\nx = np.random.randn(n) * 2 + 10\ny = x * 0.8 + np.random.randn(n) * 1.5 + 2\n\ndf = pd.DataFrame({\"x\": x, \"y\": y})\n\n# Calculate axis limits for aligned marginal plots\nx_min, x_max = df[\"x\"].min() - 0.5, df[\"x\"].max() + 0.5\ny_min, y_max = df[\"y\"].min() - 0.5, df[\"y\"].max() + 0.5\n\n# Main scatter plot\nmain_scatter = (\n    ggplot(df, aes(x=\"x\", y=\"y\"))\n    + geom_point(color=BRAND, size=4, alpha=0.65)\n    + labs(\n        x=\"Measurement A\", y=\"Measurement B\", title=\"scatter-marginal · letsplot · anyplot.ai\"\n    )\n    + scale_x_continuous(limits=[x_min, x_max])\n    + scale_y_continuous(limits=[y_min, y_max])\n    + theme_minimal()\n    + theme(\n        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_background=element_rect(fill=PAGE_BG),\n        panel_grid_major=element_line(color=INK_SOFT, size=0.2),\n        plot_title=element_text(size=24, color=INK),\n        axis_title=element_text(size=20, color=INK),\n        axis_text=element_text(size=16, color=INK_SOFT),\n        axis_line=element_line(color=INK_SOFT),\n    )\n)\n\n# Top marginal histogram with KDE overlay\ntop_hist = (\n    ggplot(df, aes(x=\"x\"))\n    + geom_histogram(\n        aes(y=\"..density..\"),\n        fill=BRAND,\n        color=PAGE_BG,\n        alpha=0.5,\n        bins=25,\n    )\n    + geom_density(color=ACCENT, size=1.5, alpha=0.8)\n    + scale_x_continuous(limits=[x_min, x_max])\n    + theme_minimal()\n    + theme(\n        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_background=element_rect(fill=PAGE_BG),\n        panel_grid_major=element_line(color=INK_SOFT, size=0.15),\n        axis_title=element_blank(),\n        axis_text_x=element_blank(),\n        axis_ticks_x=element_blank(),\n        axis_text_y=element_text(size=14, color=INK_SOFT),\n        axis_line_x=element_blank(),\n    )\n)\n\n# Right marginal histogram with KDE overlay\nright_hist = (\n    ggplot(df, aes(x=\"y\"))\n    + geom_histogram(\n        aes(y=\"..density..\"),\n        fill=BRAND,\n        color=PAGE_BG,\n        alpha=0.5,\n        bins=25,\n    )\n    + geom_density(color=ACCENT, size=1.5, alpha=0.8)\n    + coord_flip()\n    + scale_x_continuous(limits=[y_min, y_max])\n    + theme_minimal()\n    + theme(\n        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_background=element_rect(fill=PAGE_BG),\n        panel_grid_major=element_line(color=INK_SOFT, size=0.15),\n        axis_title=element_blank(),\n        axis_text_y=element_blank(),\n        axis_ticks_y=element_blank(),\n        axis_text_x=element_text(size=14, color=INK_SOFT),\n        axis_line_y=element_blank(),\n    )\n)\n\n# Combine using ggbunch\ncombined = (\n    ggbunch(\n        [top_hist, main_scatter, right_hist],\n        [\n            (0, 0.0, 0.76, 0.18),  # Top histogram\n            (0, 0.18, 0.76, 0.82),  # Main scatter plot\n            (0.76, 0.18, 0.24, 0.82),  # Right histogram (aligned to bottom of main plot)\n        ],\n    )\n    + ggsize(1600, 900)\n)\n\n# Save as PNG (scale 3x to get 4800 x 2700 px)\nexport_ggsave(combined, filename=f\"plot-{THEME}.png\", path=\".\", scale=3)\n\n# Save HTML for interactive version\nexport_ggsave(combined, filename=f\"plot-{THEME}.html\", path=\".\")\n"}