{"spec_id":"scatter-marginal","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\nscatter-marginal: Scatter Plot with Marginal Distributions\nLibrary: plotly 6.7.0 | Python 3.13.13\nQuality: 92/100 | Updated: 2026-05-09\n\"\"\"\n\nimport os\nimport sys\n\nimport numpy as np\n\n\ntry:\n    import plotly.graph_objects as go\nexcept ImportError:\n    __file__ = os.path.abspath(__file__)\n    __dir__ = os.path.dirname(__file__)\n    if __dir__ in sys.path:\n        sys.path.remove(__dir__)\n    import plotly.graph_objects as go\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\"\nGRID = \"rgba(26,26,23,0.10)\" if THEME == \"light\" else \"rgba(240,239,232,0.10)\"\nBRAND = \"#009E73\"\n\n# Data - Bivariate normal with correlation (scientific measurement scenario)\nnp.random.seed(42)\nn = 200\n# Temperature (°C) and relative humidity (%)\ntemperature = np.random.randn(n) * 3.5 + 22\nhumidity = 0.65 * temperature + np.random.randn(n) * 6 + 35\n\n# Create scatter plot with marginal histograms using go (for full control)\nfig = go.Figure()\n\n# Add scatter trace\nfig.add_trace(\n    go.Scatter(\n        x=temperature,\n        y=humidity,\n        mode=\"markers\",\n        marker=dict(size=14, color=BRAND, opacity=0.65, line=dict(width=1, color=PAGE_BG)),\n        name=\"Measurements\",\n        showlegend=False,\n    )\n)\n\n# Add marginal histogram for X (temperature)\nfig.add_trace(\n    go.Histogram(\n        x=temperature,\n        name=\"Temp Distribution\",\n        marker=dict(color=BRAND, opacity=0.6),\n        yaxis=\"y2\",\n        xaxis=\"x\",\n        showlegend=False,\n        nbinsx=25,\n    )\n)\n\n# Add marginal histogram for Y (humidity)\nfig.add_trace(\n    go.Histogram(\n        y=humidity,\n        name=\"Humidity Distribution\",\n        marker=dict(color=BRAND, opacity=0.6),\n        xaxis=\"x2\",\n        yaxis=\"y\",\n        showlegend=False,\n        nbinsy=25,\n        orientation=\"h\",\n    )\n)\n\n# Enhanced hover tooltips for better data storytelling\nfig.update_traces(\n    hovertemplate=\"<b>Measurement</b><br>Temperature: %{x:.1f}°C<br>Humidity: %{y:.1f}%<extra></extra>\",\n    selector=dict(mode=\"markers\"),\n)\n\n# Update layout with refined design: spine removal, subtle grid, publication-grade styling\nfig.update_layout(\n    title=dict(text=\"scatter-marginal · plotly · anyplot.ai\", font=dict(size=28, color=INK), x=0.5, xanchor=\"center\"),\n    xaxis=dict(\n        domain=[0, 0.85],\n        title=dict(text=\"Temperature (°C)\", font=dict(size=22, color=INK)),\n        tickfont=dict(size=18, color=INK_SOFT),\n        gridcolor=GRID,\n        linecolor=INK_SOFT,\n        showgrid=True,\n        gridwidth=0.5,\n        showline=True,\n        linewidth=1.5,\n        mirror=False,\n        side=\"bottom\",\n    ),\n    yaxis=dict(\n        domain=[0, 0.85],\n        title=dict(text=\"Relative Humidity (%)\", font=dict(size=22, color=INK)),\n        tickfont=dict(size=18, color=INK_SOFT),\n        gridcolor=GRID,\n        linecolor=INK_SOFT,\n        showgrid=True,\n        gridwidth=0.5,\n        showline=True,\n        linewidth=1.5,\n        mirror=False,\n        side=\"left\",\n    ),\n    xaxis2=dict(\n        domain=[0.85, 1], showticklabels=False, gridcolor=GRID, linecolor=INK_SOFT, showgrid=False, showline=False\n    ),\n    yaxis2=dict(\n        domain=[0.85, 1], showticklabels=False, gridcolor=GRID, linecolor=INK_SOFT, showgrid=False, showline=False\n    ),\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    font=dict(family=\"Arial, sans-serif\", color=INK),\n    showlegend=False,\n    margin=dict(l=120, r=80, t=100, b=100),\n    height=900,\n    width=1600,\n    hovermode=\"closest\",\n)\n\n# Save\nfig.write_image(f\"plot-{THEME}.png\", width=1600, height=900, scale=3)\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}