|
1 | | -""" pyplots.ai |
| 1 | +""" anyplot.ai |
2 | 2 | scatter-marginal: Scatter Plot with Marginal Distributions |
3 | | -Library: plotly 6.5.0 | Python 3.13.11 |
4 | | -Quality: 92/100 | Created: 2025-12-26 |
| 3 | +Library: plotly 6.7.0 | Python 3.13.13 |
| 4 | +Quality: 92/100 | Updated: 2026-05-09 |
5 | 5 | """ |
6 | 6 |
|
| 7 | +import os |
| 8 | +import sys |
| 9 | + |
7 | 10 | import numpy as np |
8 | | -import plotly.express as px |
9 | 11 |
|
10 | 12 |
|
11 | | -# Data - Bivariate normal with correlation |
| 13 | +try: |
| 14 | + import plotly.graph_objects as go |
| 15 | +except ImportError: |
| 16 | + __file__ = os.path.abspath(__file__) |
| 17 | + __dir__ = os.path.dirname(__file__) |
| 18 | + if __dir__ in sys.path: |
| 19 | + sys.path.remove(__dir__) |
| 20 | + import plotly.graph_objects as go |
| 21 | + |
| 22 | +# Theme tokens |
| 23 | +THEME = os.getenv("ANYPLOT_THEME", "light") |
| 24 | +PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17" |
| 25 | +ELEVATED_BG = "#FFFDF6" if THEME == "light" else "#242420" |
| 26 | +INK = "#1A1A17" if THEME == "light" else "#F0EFE8" |
| 27 | +INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0" |
| 28 | +GRID = "rgba(26,26,23,0.10)" if THEME == "light" else "rgba(240,239,232,0.10)" |
| 29 | +BRAND = "#009E73" |
| 30 | + |
| 31 | +# Data - Bivariate normal with correlation (scientific measurement scenario) |
12 | 32 | np.random.seed(42) |
13 | 33 | n = 200 |
14 | | -x = np.random.randn(n) * 10 + 50 |
15 | | -y = 0.7 * x + np.random.randn(n) * 8 + 15 |
| 34 | +# Temperature (°C) and relative humidity (%) |
| 35 | +temperature = np.random.randn(n) * 3.5 + 22 |
| 36 | +humidity = 0.65 * temperature + np.random.randn(n) * 6 + 35 |
16 | 37 |
|
17 | | -# Create scatter plot with marginal histograms |
18 | | -fig = px.scatter(x=x, y=y, marginal_x="histogram", marginal_y="histogram") |
| 38 | +# Create scatter plot with marginal histograms using go (for full control) |
| 39 | +fig = go.Figure() |
19 | 40 |
|
20 | | -# Style main scatter plot |
21 | | -fig.update_traces( |
22 | | - marker={"size": 14, "color": "#306998", "opacity": 0.65, "line": {"width": 1, "color": "#1e4a6e"}}, |
23 | | - selector={"type": "scatter", "mode": "markers"}, |
| 41 | +# Add scatter trace |
| 42 | +fig.add_trace( |
| 43 | + go.Scatter( |
| 44 | + x=temperature, |
| 45 | + y=humidity, |
| 46 | + mode="markers", |
| 47 | + marker=dict(size=14, color=BRAND, opacity=0.65, line=dict(width=1, color=PAGE_BG)), |
| 48 | + name="Measurements", |
| 49 | + showlegend=False, |
| 50 | + ) |
| 51 | +) |
| 52 | + |
| 53 | +# Add marginal histogram for X (temperature) |
| 54 | +fig.add_trace( |
| 55 | + go.Histogram( |
| 56 | + x=temperature, |
| 57 | + name="Temp Distribution", |
| 58 | + marker=dict(color=BRAND, opacity=0.6), |
| 59 | + yaxis="y2", |
| 60 | + xaxis="x", |
| 61 | + showlegend=False, |
| 62 | + nbinsx=25, |
| 63 | + ) |
| 64 | +) |
| 65 | + |
| 66 | +# Add marginal histogram for Y (humidity) |
| 67 | +fig.add_trace( |
| 68 | + go.Histogram( |
| 69 | + y=humidity, |
| 70 | + name="Humidity Distribution", |
| 71 | + marker=dict(color=BRAND, opacity=0.6), |
| 72 | + xaxis="x2", |
| 73 | + yaxis="y", |
| 74 | + showlegend=False, |
| 75 | + nbinsy=25, |
| 76 | + orientation="h", |
| 77 | + ) |
24 | 78 | ) |
25 | 79 |
|
26 | | -# Style marginal histograms |
| 80 | +# Enhanced hover tooltips for better data storytelling |
27 | 81 | fig.update_traces( |
28 | | - marker={"color": "#306998", "opacity": 0.7, "line": {"width": 1, "color": "#1e4a6e"}}, |
29 | | - selector={"type": "histogram"}, |
| 82 | + hovertemplate="<b>Measurement</b><br>Temperature: %{x:.1f}°C<br>Humidity: %{y:.1f}%<extra></extra>", |
| 83 | + selector=dict(mode="markers"), |
30 | 84 | ) |
31 | 85 |
|
32 | | -# Layout for 4800x2700 px |
| 86 | +# Update layout with refined design: spine removal, subtle grid, publication-grade styling |
33 | 87 | fig.update_layout( |
34 | | - title={ |
35 | | - "text": "scatter-marginal \u00b7 plotly \u00b7 pyplots.ai", |
36 | | - "font": {"size": 32}, |
37 | | - "x": 0.5, |
38 | | - "xanchor": "center", |
39 | | - }, |
40 | | - xaxis={ |
41 | | - "title": {"text": "X Value", "font": {"size": 24}}, |
42 | | - "tickfont": {"size": 18}, |
43 | | - "gridcolor": "rgba(128, 128, 128, 0.3)", |
44 | | - "gridwidth": 1, |
45 | | - }, |
46 | | - yaxis={ |
47 | | - "title": {"text": "Y Value", "font": {"size": 24}}, |
48 | | - "tickfont": {"size": 18}, |
49 | | - "gridcolor": "rgba(128, 128, 128, 0.3)", |
50 | | - "gridwidth": 1, |
51 | | - }, |
52 | | - xaxis2={"tickfont": {"size": 14}}, |
53 | | - yaxis2={"tickfont": {"size": 14}}, |
54 | | - xaxis3={"tickfont": {"size": 14}}, |
55 | | - yaxis3={"tickfont": {"size": 14}}, |
56 | | - template="plotly_white", |
57 | | - font={"family": "Arial, sans-serif"}, |
| 88 | + title=dict(text="scatter-marginal · plotly · anyplot.ai", font=dict(size=28, color=INK), x=0.5, xanchor="center"), |
| 89 | + xaxis=dict( |
| 90 | + domain=[0, 0.85], |
| 91 | + title=dict(text="Temperature (°C)", font=dict(size=22, color=INK)), |
| 92 | + tickfont=dict(size=18, color=INK_SOFT), |
| 93 | + gridcolor=GRID, |
| 94 | + linecolor=INK_SOFT, |
| 95 | + showgrid=True, |
| 96 | + gridwidth=0.5, |
| 97 | + showline=True, |
| 98 | + linewidth=1.5, |
| 99 | + mirror=False, |
| 100 | + side="bottom", |
| 101 | + ), |
| 102 | + yaxis=dict( |
| 103 | + domain=[0, 0.85], |
| 104 | + title=dict(text="Relative Humidity (%)", font=dict(size=22, color=INK)), |
| 105 | + tickfont=dict(size=18, color=INK_SOFT), |
| 106 | + gridcolor=GRID, |
| 107 | + linecolor=INK_SOFT, |
| 108 | + showgrid=True, |
| 109 | + gridwidth=0.5, |
| 110 | + showline=True, |
| 111 | + linewidth=1.5, |
| 112 | + mirror=False, |
| 113 | + side="left", |
| 114 | + ), |
| 115 | + xaxis2=dict( |
| 116 | + domain=[0.85, 1], showticklabels=False, gridcolor=GRID, linecolor=INK_SOFT, showgrid=False, showline=False |
| 117 | + ), |
| 118 | + yaxis2=dict( |
| 119 | + domain=[0.85, 1], showticklabels=False, gridcolor=GRID, linecolor=INK_SOFT, showgrid=False, showline=False |
| 120 | + ), |
| 121 | + paper_bgcolor=PAGE_BG, |
| 122 | + plot_bgcolor=PAGE_BG, |
| 123 | + font=dict(family="Arial, sans-serif", color=INK), |
58 | 124 | showlegend=False, |
59 | | - margin={"l": 100, "r": 100, "t": 120, "b": 100}, |
| 125 | + margin=dict(l=120, r=80, t=100, b=100), |
| 126 | + height=900, |
| 127 | + width=1600, |
| 128 | + hovermode="closest", |
60 | 129 | ) |
61 | 130 |
|
62 | 131 | # Save |
63 | | -fig.write_image("plot.png", width=1600, height=900, scale=3) |
64 | | -fig.write_html("plot.html", include_plotlyjs="cdn") |
| 132 | +fig.write_image(f"plot-{THEME}.png", width=1600, height=900, scale=3) |
| 133 | +fig.write_html(f"plot-{THEME}.html", include_plotlyjs="cdn") |
0 commit comments