{"spec_id":"scatter-complex-plane","library":"makie","language":"julia","code":"# anyplot.ai\n# scatter-complex-plane: Complex Plane Visualization (Argand Diagram)\n# Library: makie 0.24.16 | Julia 1.13.4\n# Quality: 86/100 | Updated: 2026-11-28\n\nusing CairoMakie\nusing Colors\nusing DataFrames\n\n# Theme tokens\nconst THEME       = get(ENV, \"ANYPLOT_THEME\", \"light\")\nconst PAGE_BG     = THEME == \"light\" ? colorant\"#FAF8F1\" : colorant\"#1A1A17\"\nconst ELEVATED_BG = THEME == \"light\" ? colorant\"#FFFDF6\" : colorant\"#242420\"\nconst INK         = THEME == \"light\" ? colorant\"#1A1A17\" : colorant\"#F0EFE8\"\nconst INK_SOFT    = THEME == \"light\" ? colorant\"#4A4A44\" : colorant\"#B8B7B0\"\n# Imprint palette\nconst IMPRINT_PALETTE = [\n    colorant\"#009E73\",\n    colorant\"#C475FD\",\n    colorant\"#4467A3\",\n    colorant\"#BD8233\",\n    colorant\"#AE3030\",\n    colorant\"#2ABCCD\",\n    colorant\"#954477\",\n    colorant\"#99B314\",\n]\n\n# Columns\nconst NAME     = \"Name\"                # role: label\nconst RE_PART  = \"Real part Re(z)\"     # role: x\nconst IM_PART  = \"Imaginary part Im(z)\" # role: y\nconst KIND     = \"Kind of number\"      # role: group\n\n# Data: sixth roots of unity, two operands a and b, and their sum and product\nroots = [cis(2π * k / 6) for k in 0:5]\na = 1.2 + 0.8im\nb = -1.0 + 0.6im\nz = vcat(roots, [a, b, a + b, a * b])\ndf = DataFrame(\n    NAME => vcat([\"z\" * Char(0x2080 + k) for k in 0:5], [\"a\", \"b\", \"a + b\", \"a · b\"]),\n    RE_PART => real.(z),\n    IM_PART => imag.(z),\n    KIND => vcat(fill(\"6th roots of unity\", 6), fill(\"Operands\", 2), fill(\"Sum and product\", 2)),\n)\n\n# Plot\nkinds   = unique(df[!, KIND])\ngroup   = [findfirst(==(k), kinds) for k in df[!, KIND]]\nmarkers = [:circle, :diamond, :rect][group]\nre_vals = df[!, RE_PART]\nim_vals = df[!, IM_PART]\nspan    = maximum(abs.(vcat(re_vals, im_vals))) * 1.4\ntheta   = range(0, 2π; length = 300)\n\nfig = Figure(size = (1200, 1200), fontsize = 14, backgroundcolor = PAGE_BG)\n\nax = Axis(\n    fig[1, 1];\n    title              = \"scatter-complex-plane · julia · makie · anyplot.ai\",\n    titlesize          = 20,\n    titlecolor         = INK,\n    xlabel             = RE_PART,\n    ylabel             = IM_PART,\n    xlabelsize         = 16,\n    ylabelsize         = 16,\n    xlabelcolor        = INK,\n    ylabelcolor        = INK,\n    xticklabelsize     = 14,\n    yticklabelsize     = 14,\n    xticklabelcolor    = INK_SOFT,\n    yticklabelcolor    = INK_SOFT,\n    xtickcolor         = INK_SOFT,\n    ytickcolor         = INK_SOFT,\n    backgroundcolor    = PAGE_BG,\n    topspinevisible    = false,\n    rightspinevisible  = false,\n    leftspinecolor     = INK_SOFT,\n    bottomspinecolor   = INK_SOFT,\n    xgridcolor         = (INK_SOFT, 0.12),\n    ygridcolor         = (INK_SOFT, 0.12),\n    xminorgridvisible  = false,\n    yminorgridvisible  = false,\n    aspect             = DataAspect(),\n)\nlimits!(ax, -span, span, -span, span)\n\n# Unit circle, dashed reference\nlines!(ax, cos.(theta), sin.(theta); linestyle = :dash, color = (INK_SOFT, 0.8), linewidth = 2)\n\n# Real and imaginary axes through the origin\nhlines!(ax, [0.0]; color = (INK_SOFT, 0.5), linewidth = 1)\nvlines!(ax, [0.0]; color = (INK_SOFT, 0.5), linewidth = 1)\n\n# One arrow per point, from the origin to its head\narrows2d!(ax, zeros(nrow(df)), zeros(nrow(df)), re_vals, im_vals;\n    color     = IMPRINT_PALETTE[group],\n    shaftwidth = 2.5,\n    tipwidth  = 14,\n    tiplength = 16,\n)\n\n# Points, one scatter per kind so the legend names them\nfor (i, kind) in enumerate(kinds)\n    idx = findall(==(i), group)\n    scatter!(ax, re_vals[idx], im_vals[idx];\n        color       = IMPRINT_PALETTE[i],\n        marker      = markers[idx[1]],\n        markersize  = 16,\n        strokewidth = 1.5,\n        strokecolor = PAGE_BG,\n        label       = kind,\n    )\nend\n\n# Name and rectangular form a+bi, placed outward along each point's direction\nfor (name, x, y) in zip(df[!, NAME], re_vals, im_vals)\n    r    = hypot(x, y)\n    sign = y >= 0 ? \"+\" : \"−\"\n    form = string(replace(string(round(x; digits = 2)), \"-\" => \"−\"), sign, round(abs(y); digits = 2), \"i\")\n    h    = x > 0.1 ? :left : (x < -0.1 ? :right : :center)\n    v    = y > -0.05 ? :bottom : :top\n    text!(ax, x + 0.1 * x / r, y + 0.1 * y / r;\n        text     = name * \"\\n\" * form,\n        fontsize = 15,\n        color    = INK,\n        align    = (h, v),\n    )\nend\n\naxislegend(ax;\n    position        = :lb,\n    backgroundcolor = ELEVATED_BG,\n    framecolor      = INK_SOFT,\n    labelcolor      = INK,\n    labelsize       = 15,\n)\n\n# Save\nsave(\"plot-$(THEME).png\", fig; px_per_unit = 2)\n"}