Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 55 additions & 35 deletions plots/contour-decision-boundary/implementations/python/plotly.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
""" pyplots.ai
""" anyplot.ai
contour-decision-boundary: Decision Boundary Classifier Visualization
Library: plotly 6.5.0 | Python 3.13.11
Quality: 92/100 | Created: 2025-12-31
Library: plotly 6.7.0 | Python 3.13.13
Quality: 95/100 | Updated: 2026-05-16
"""

import os

import numpy as np
import plotly.graph_objects as go
from sklearn.datasets import make_moons
from sklearn.neighbors import KNeighborsClassifier
from sklearn.preprocessing import StandardScaler


THEME = os.getenv("ANYPLOT_THEME", "light")
PAGE_BG = "#FAF8F1" if THEME == "light" else "#1A1A17"
ELEVATED_BG = "#FFFDF6" if THEME == "light" else "#242420"
INK = "#1A1A17" if THEME == "light" else "#F0EFE8"
INK_SOFT = "#4A4A44" if THEME == "light" else "#B8B7B0"
GRID = "rgba(26,26,23,0.10)" if THEME == "light" else "rgba(240,239,232,0.10)"

OKABE_ITO = ["#009E73", "#D55E00", "#0072B2", "#CC79A7"]

# Data - Generate moon-shaped classification data
np.random.seed(42)
X, y = make_moons(n_samples=200, noise=0.25, random_state=42)
Expand All @@ -28,11 +39,7 @@
y_min, y_max = X[:, 1].min() - 0.5, X[:, 1].max() + 0.5
xx, yy = np.meshgrid(np.linspace(x_min, x_max, 150), np.linspace(y_min, y_max, 150))

# Get predictions for mesh grid
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)

# Get prediction probabilities for smoother contours
# Get prediction probabilities for smooth contours
Z_prob = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1]
Z_prob = Z_prob.reshape(xx.shape)

Expand All @@ -45,14 +52,18 @@
x=np.linspace(x_min, x_max, 150),
y=np.linspace(y_min, y_max, 150),
z=Z_prob,
colorscale=[[0, "#306998"], [1, "#FFD43B"]],
opacity=0.6,
colorscale=[[0, OKABE_ITO[0]], [1, OKABE_ITO[1]]],
opacity=0.4,
showscale=True,
colorbar=dict(
title=dict(text="Class Probability", font=dict(size=18)), tickfont=dict(size=16), len=0.7, thickness=25
title=dict(text="Class Probability", font=dict(size=18)),
tickfont=dict(size=16),
len=0.7,
thickness=25,
bordercolor=INK_SOFT,
),
contours=dict(showlines=False),
hoverinfo="skip",
hovertemplate="Feature 1: %{x:.2f}<br>Feature 2: %{y:.2f}<br>Probability: %{z:.2f}<extra></extra>",
)
)

Expand All @@ -64,7 +75,7 @@
z=Z_prob,
showscale=False,
contours=dict(start=0.5, end=0.5, size=0.1, coloring="lines", showlabels=False),
line=dict(color="white", width=3, dash="dash"),
line=dict(color=INK_SOFT, width=3, dash="dash"),
hoverinfo="skip",
)
)
Expand All @@ -79,9 +90,9 @@
x=X_class0[:, 0],
y=X_class0[:, 1],
mode="markers",
marker=dict(size=14, color="#306998", line=dict(color="white", width=2), symbol="circle"),
marker=dict(size=14, color=OKABE_ITO[0], line=dict(color=PAGE_BG, width=2), symbol="circle"),
name="Class 0",
hovertemplate="Feature 1: %{x:.2f}<br>Feature 2: %{y:.2f}<extra>Class 0</extra>",
hovertemplate="Feature 1: %{x:.2f}<br>Feature 2: %{y:.2f}<br>Class: 0<extra></extra>",
)
)

Expand All @@ -91,47 +102,56 @@
x=X_class1[:, 0],
y=X_class1[:, 1],
mode="markers",
marker=dict(size=14, color="#FFD43B", line=dict(color="black", width=2), symbol="diamond"),
marker=dict(size=14, color=OKABE_ITO[1], line=dict(color=PAGE_BG, width=2), symbol="diamond"),
name="Class 1",
hovertemplate="Feature 1: %{x:.2f}<br>Feature 2: %{y:.2f}<extra>Class 1</extra>",
hovertemplate="Feature 1: %{x:.2f}<br>Feature 2: %{y:.2f}<br>Class: 1<extra></extra>",
)
)

# Update layout
# Update layout with theme-adaptive colors
fig.update_layout(
title=dict(text="contour-decision-boundary · plotly · pyplots.ai", font=dict(size=28), x=0.5, xanchor="center"),
title=dict(
text="contour-decision-boundary · plotly · anyplot.ai", font=dict(size=28, color=INK), x=0.5, xanchor="center"
),
xaxis=dict(
title=dict(text="Feature 1 (Standardized)", font=dict(size=22)),
tickfont=dict(size=18),
title=dict(text="Feature 1 (Standardized)", font=dict(size=22, color=INK)),
tickfont=dict(size=18, color=INK_SOFT),
showgrid=True,
gridwidth=1,
gridcolor="rgba(128, 128, 128, 0.3)",
gridcolor=GRID,
zeroline=False,
linecolor=INK_SOFT,
linewidth=2,
),
yaxis=dict(
title=dict(text="Feature 2 (Standardized)", font=dict(size=22)),
tickfont=dict(size=18),
title=dict(text="Feature 2 (Standardized)", font=dict(size=22, color=INK)),
tickfont=dict(size=18, color=INK_SOFT),
showgrid=True,
gridwidth=1,
gridcolor="rgba(128, 128, 128, 0.3)",
gridcolor=GRID,
zeroline=False,
linecolor=INK_SOFT,
linewidth=2,
scaleanchor="x",
scaleratio=1,
),
template="plotly_white",
paper_bgcolor=PAGE_BG,
plot_bgcolor=PAGE_BG,
font=dict(color=INK),
legend=dict(
font=dict(size=18),
x=0.02,
y=0.98,
xanchor="left",
yanchor="top",
bgcolor="rgba(255, 255, 255, 0.8)",
bordercolor="rgba(0, 0, 0, 0.3)",
font=dict(size=18, color=INK_SOFT),
x=0.98,
y=0.02,
xanchor="right",
yanchor="bottom",
bgcolor=ELEVATED_BG,
bordercolor=INK_SOFT,
borderwidth=1,
),
margin=dict(l=80, r=100, t=100, b=80),
hovermode="closest",
)

# Save as PNG and HTML
fig.write_image("plot.png", width=1600, height=900, scale=3)
fig.write_html("plot.html", include_plotlyjs="cdn")
fig.write_image(f"plot-{THEME}.png", width=1600, height=900, scale=3)
fig.write_html(f"plot-{THEME}.html", include_plotlyjs="cdn")
Loading
Loading