{"spec_id":"scatter-complex-plane","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nscatter-complex-plane: Complex Plane Visualization (Argand Diagram)\nLibrary: matplotlib 3.11.2 | Python 3.14.10\nQuality: 87/100 | Updated: 2026-11-28\n\"\"\"\n\nimport sys\n\n\nsys.path.pop(0)  # prevent this file from shadowing the matplotlib package\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nfrom matplotlib.lines import Line2D\nfrom matplotlib.patches import Arc\n\n\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\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\n\n# Imprint palette — 8 hues, canonical order\nIMPRINT_PALETTE = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\"]\n\n# Columns: set these to your table's column names\nREAL = \"Real axis\"  # role: real\nIMAGINARY = \"Imaginary axis\"  # role: imaginary\nLABEL = \"Point\"  # role: label\nCATEGORY = \"Category\"  # role: extra\n\n# Data: replace this block with your own table, for example df = pd.read_csv(\"complex_numbers.csv\")\nn_roots = 5\nroots = np.exp(2j * np.pi * np.arange(n_roots) / n_roots)\narbitrary = np.array([1.6 + 0.7j, -0.4 + 1.5j])\nz_all = np.concatenate([roots, arbitrary])\ndf = pd.DataFrame(\n    {\n        REAL: z_all.real,\n        IMAGINARY: z_all.imag,\n        LABEL: [\"1\", \"ω\", \"ω²\", \"ω³\", \"ω⁴\", \"z₁\", \"z₂\"],\n        CATEGORY: [\"5th roots of unity (ω = e^(2πi/5))\"] * n_roots + [\"Arbitrary complex numbers\"] * len(arbitrary),\n    }\n)\n\n# Plot — square canvas required so the unit circle appears circular\ngroups = list(dict.fromkeys(df[CATEGORY]))\nmarker_cycle = [\"o\", \"D\", \"s\", \"^\", \"v\", \"P\", \"X\", \"*\"]\ngroup_color = {g: IMPRINT_PALETTE[i % len(IMPRINT_PALETTE)] for i, g in enumerate(groups)}\ngroup_marker = {g: marker_cycle[i % len(marker_cycle)] for i, g in enumerate(groups)}\nall_points = df[REAL].to_numpy() + 1j * df[IMAGINARY].to_numpy()\nall_colors = [group_color[g] for g in df[CATEGORY]]\nall_markers = [group_marker[g] for g in df[CATEGORY]]\nall_labels = df[LABEL].tolist()\nextent = max(df[REAL].abs().max(), df[IMAGINARY].abs().max(), 1.0) * 1.5\n\nfig, ax = plt.subplots(figsize=(6, 6), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\nax.set_aspect(\"equal\")\n\n# Unit circle — dashed geometric reference\ntheta = np.linspace(0, 2 * np.pi, 400)\nax.plot(np.cos(theta), np.sin(theta), color=INK_MUTED, linewidth=1.5, linestyle=\"--\", alpha=0.7, zorder=1)\n\n# Real and imaginary axes through the origin\nax.axhline(0, color=INK_SOFT, linewidth=0.9, alpha=0.6, zorder=1)\nax.axvline(0, color=INK_SOFT, linewidth=0.9, alpha=0.6, zorder=1)\n\n# Subtle grid for both axes\nax.xaxis.grid(True, alpha=0.12, linewidth=0.6, color=INK, zorder=0)\nax.yaxis.grid(True, alpha=0.12, linewidth=0.6, color=INK, zorder=0)\n\n# Arrows from origin to each complex number\nfor z, color in zip(all_points, all_colors, strict=False):\n    ax.annotate(\n        \"\",\n        xy=(z.real, z.imag),\n        xytext=(0.0, 0.0),\n        arrowprops={\"arrowstyle\": \"->\", \"color\": color, \"lw\": 1.8, \"mutation_scale\": 18, \"shrinkA\": 0, \"shrinkB\": 7},\n        zorder=3,\n    )\n\n# Angle arcs — dotted arcs from positive real axis to each vector, showing the argument\nfor z, color in zip(all_points, all_colors, strict=False):\n    angle_deg = np.degrees(np.angle(z))  # in [-180, 180]\n    if abs(angle_deg) > 0.5:\n        arc = Arc(\n            (0, 0),\n            0.38,\n            0.38,\n            angle=0,\n            theta1=min(0.0, angle_deg),\n            theta2=max(0.0, angle_deg),\n            color=color,\n            linewidth=0.9,\n            linestyle=\":\",\n            alpha=0.45,\n            zorder=2,\n        )\n        ax.add_patch(arc)\n\n# Scatter points\nfor z, color, marker in zip(all_points, all_colors, all_markers, strict=False):\n    ax.scatter(z.real, z.imag, s=130, color=color, marker=marker, edgecolors=PAGE_BG, linewidth=1.2, zorder=5)\n\n# Labels — name, rectangular form (a+bi), and polar form (|z|∠θ°), offset outward from origin\nfor z, label in zip(all_points, all_labels, strict=False):\n    re, im = z.real, z.imag\n    mag = abs(z) + 1e-12\n    angle_deg = np.degrees(np.angle(z)) % 360\n\n    if abs(im) < 1e-10:\n        coord = f\"{re:.2f}\"\n    elif im >= 0:\n        coord = f\"{re:.2f}+{im:.2f}i\"\n    else:\n        coord = f\"{re:.2f}{im:.2f}i\"\n\n    polar = f\"{abs(z):.2f}∠{angle_deg:.1f}°\"\n\n    ux, uy = re / mag, im / mag\n    ha = \"left\" if ux > 0.35 else \"right\" if ux < -0.35 else \"center\"\n    va = \"bottom\" if uy > 0.35 else \"top\" if uy < -0.35 else \"center\"\n\n    ax.annotate(\n        f\"{label}\\n{coord}\\n{polar}\",\n        xy=(re, im),\n        xytext=(ux * 14, uy * 14),\n        textcoords=\"offset points\",\n        fontsize=9,\n        color=INK,\n        ha=ha,\n        va=va,\n        bbox={\"facecolor\": ELEVATED_BG, \"edgecolor\": \"none\", \"alpha\": 0.88, \"pad\": 3},\n        zorder=6,\n    )\n\n# Style\ntitle = \"scatter-complex-plane · python · matplotlib · anyplot.ai\"\nn_chars = len(title)\nratio = 67 / n_chars if n_chars > 67 else 1.0\ntitle_fontsize = max(8, round(12 * ratio))\n\nax.set_title(title, fontsize=title_fontsize, fontweight=\"medium\", color=INK, pad=10)\nax.set_xlabel(REAL, fontsize=10, color=INK)\nax.set_ylabel(IMAGINARY, fontsize=10, color=INK)\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT)\n\nax.spines[\"top\"].set_visible(False)\nax.spines[\"right\"].set_visible(False)\nax.spines[\"left\"].set_color(INK_SOFT)\nax.spines[\"bottom\"].set_color(INK_SOFT)\n\nax.set_xlim(-extent, extent)\nax.set_ylim(-extent, extent)\n\n# Legend\nlegend_elements = [\n    Line2D(\n        [0],\n        [0],\n        marker=group_marker[g],\n        linestyle=\"none\",\n        markerfacecolor=group_color[g],\n        markeredgecolor=PAGE_BG,\n        markersize=9,\n        label=g,\n    )\n    for g in groups\n] + [Line2D([0], [0], color=INK_MUTED, linewidth=1.5, linestyle=\"--\", label=\"Unit circle  |z| = 1\")]\nleg = ax.legend(handles=legend_elements, fontsize=8, loc=\"lower left\", framealpha=0.9)\nleg.get_frame().set_facecolor(ELEVATED_BG)\nleg.get_frame().set_edgecolor(INK_SOFT)\nplt.setp(leg.get_texts(), color=INK_SOFT)\n\n# Save\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\nplt.close()\n"}