{"spec_id":"contour-decision-boundary","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\ncontour-decision-boundary: Decision Boundary Classifier Visualization\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 94/100 | Updated: 2026-05-16\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom sklearn.datasets import make_moons\nfrom sklearn.svm import SVC\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\"\n\nIMPRINT = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\"]\n\n# Data - generate synthetic two-moon classification data\nnp.random.seed(42)\nX, y = make_moons(n_samples=200, noise=0.25, random_state=42)\n\n# Train a classifier (SVM with RBF kernel)\nclassifier = SVC(kernel=\"rbf\", C=1.0, gamma=\"scale\")\nclassifier.fit(X, y)\n\n# Create mesh grid for decision boundary\nx_min, x_max = X[:, 0].min() - 0.5, X[:, 0].max() + 0.5\ny_min, y_max = X[:, 1].min() - 0.5, X[:, 1].max() + 0.5\nxx, yy = np.meshgrid(np.linspace(x_min, x_max, 200), np.linspace(y_min, y_max, 200))\n\n# Predict class for each point in mesh\nZ = classifier.predict(np.c_[xx.ravel(), yy.ravel()])\nZ = Z.reshape(xx.shape)\n\n# Plot\nfig, ax = plt.subplots(figsize=(16, 9), facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Plot decision regions with contourf\nax.contourf(xx, yy, Z, alpha=0.3, colors=[IMPRINT[0], IMPRINT[1]], levels=[-0.5, 0.5, 1.5])\n\n# Add decision boundary line\nax.contour(xx, yy, Z, colors=INK_SOFT, linewidths=2, levels=[0.5])\n\n# Identify correctly and incorrectly classified points\npredictions = classifier.predict(X)\ncorrect = predictions == y\nincorrect = ~correct\n\n# Plot training points - correctly classified\nclass_0_correct = (y == 0) & correct\nclass_1_correct = (y == 1) & correct\nax.scatter(\n    X[class_0_correct, 0],\n    X[class_0_correct, 1],\n    c=IMPRINT[0],\n    s=150,\n    alpha=0.9,\n    edgecolors=PAGE_BG,\n    linewidths=2,\n    marker=\"o\",\n    label=\"Class 0 (correct)\",\n    zorder=3,\n)\nax.scatter(\n    X[class_1_correct, 0],\n    X[class_1_correct, 1],\n    c=IMPRINT[1],\n    s=150,\n    alpha=0.9,\n    edgecolors=PAGE_BG,\n    linewidths=2,\n    marker=\"o\",\n    label=\"Class 1 (correct)\",\n    zorder=3,\n)\n\n# Plot incorrectly classified points with X marker\nif np.any(incorrect):\n    class_0_incorrect = (y == 0) & incorrect\n    class_1_incorrect = (y == 1) & incorrect\n    if np.any(class_0_incorrect):\n        ax.scatter(\n            X[class_0_incorrect, 0],\n            X[class_0_incorrect, 1],\n            c=IMPRINT[0],\n            s=200,\n            alpha=0.9,\n            edgecolors=INK_SOFT,\n            linewidths=3,\n            marker=\"X\",\n            label=\"Class 0 (misclassified)\",\n            zorder=4,\n        )\n    if np.any(class_1_incorrect):\n        ax.scatter(\n            X[class_1_incorrect, 0],\n            X[class_1_incorrect, 1],\n            c=IMPRINT[1],\n            s=200,\n            alpha=0.9,\n            edgecolors=INK_SOFT,\n            linewidths=3,\n            marker=\"X\",\n            label=\"Class 1 (misclassified)\",\n            zorder=4,\n        )\n\n# Style\nax.set_xlabel(\"Feature X1\", fontsize=20, color=INK)\nax.set_ylabel(\"Feature X2\", fontsize=20, color=INK)\nax.set_title(\"contour-decision-boundary · matplotlib · anyplot.ai\", fontsize=24, fontweight=\"medium\", color=INK)\nax.tick_params(axis=\"both\", labelsize=16, colors=INK_SOFT)\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nfor s in (\"left\", \"bottom\"):\n    ax.spines[s].set_color(INK_SOFT)\nax.grid(True, alpha=0.1, linewidth=0.8, color=INK)\n\nleg = ax.legend(fontsize=14, loc=\"upper left\")\nif leg:\n    leg.get_frame().set_facecolor(ELEVATED_BG)\n    leg.get_frame().set_edgecolor(INK_SOFT)\n    plt.setp(leg.get_texts(), color=INK_SOFT)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}