{"spec_id":"energy-level-atomic","library":"makie","language":"julia","code":"# anyplot.ai\n# energy-level-atomic: Atomic Energy Level 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\nusing Printf\n\n# Theme tokens — Imprint palette, theme-adaptive chrome\nconst THEME     = get(ENV, \"ANYPLOT_THEME\", \"light\")\nconst PAGE_BG   = THEME == \"light\" ? colorant\"#FAF8F1\" : colorant\"#1A1A17\"\nconst INK       = THEME == \"light\" ? colorant\"#1A1A17\" : colorant\"#F0EFE8\"\nconst INK_SOFT  = THEME == \"light\" ? colorant\"#4A4A44\" : colorant\"#B8B7B0\"\nconst INK_MUTED = THEME == \"light\" ? colorant\"#6B6A63\" : colorant\"#A8A79F\"\n\nconst IMPRINT_PALETTE = [\n    colorant\"#009E73\",  # 1 brand green\n    colorant\"#C475FD\",  # 2 lavender\n    colorant\"#4467A3\",  # 3 blue\n    colorant\"#BD8233\",  # 4 ochre\n    colorant\"#AE3030\",  # 5 matte red\n    colorant\"#2ABCCD\",  # 6 cyan\n    colorant\"#954477\",  # 7 rose\n    colorant\"#99B314\",  # 8 lime\n]\n\n# Constants — hydrogen: E_n = -13.6 / n² eV, λ = hc / ΔE with hc = 1240 eV·nm\nconst RYDBERG_EV = 13.6\nconst HC_EV_NM   = 1240.0\n\n# Columns: set these to your table's column names\nconst UPPER_LEVEL = \"Upper level n\"     # role: from\nconst LOWER_LEVEL = \"Lower level n\"     # role: to\nconst SERIES      = \"Spectral series\"   # role: group\n\n# Data: replace this block with your own table, for example df = CSV.read(\"transitions.csv\", DataFrame)\nn_levels = 6\ndf = DataFrame(\n    UPPER_LEVEL => [2, 3, 4, 3, 4, 5, 4, 5],\n    LOWER_LEVEL => [1, 1, 1, 2, 2, 2, 3, 3],\n    SERIES => [\"Lyman (UV)\", \"Lyman (UV)\", \"Lyman (UV)\",\n               \"Balmer (visible)\", \"Balmer (visible)\", \"Balmer (visible)\",\n               \"Paschen (IR)\", \"Paschen (IR)\"],\n)\n\n# Plot: reads only df and the constants above\nlevels      = 1:n_levels\nenergies    = [-RYDBERG_EV / n^2 for n in levels]\n# Display position grows with n, so the converging upper levels stay readable\nlevel_y     = Float64.(levels)\nionization_y = n_levels + 0.9\nseries      = unique(df[!, SERIES])\nytick_pos   = [level_y; ionization_y]\nytick_labels = [@sprintf(\"%.2f\", e) for e in energies]\npush!(ytick_labels, \"0\")\n\n# Schematic horizontal layout: arrows of one series sit together\nx_line_start = 0.06\nx_line_end   = 0.90\nx_label      = 0.92\narrow_dx     = 0.085\narrow_x      = [x_line_start + 0.05 + arrow_dx * (i - 1) + 0.04 * (findfirst(==(s), series) - 1)\n                for (i, s) in enumerate(df[!, SERIES])]\n\nfig = Figure(\n    size            = (1600, 900),\n    fontsize        = 14,\n    backgroundcolor = PAGE_BG,\n)\n\nax = Axis(\n    fig[1, 1];\n    title               = \"energy-level-atomic · julia · makie · anyplot.ai\",\n    titlesize           = 22,\n    titlecolor          = INK,\n    ylabel              = \"Energy (eV)\",\n    ylabelcolor         = INK,\n    ylabelsize          = 18,\n    yticklabelcolor     = INK_SOFT,\n    yticklabelsize      = 16,\n    yticks              = (ytick_pos, ytick_labels),\n    backgroundcolor     = PAGE_BG,\n    topspinevisible     = false,\n    rightspinevisible   = false,\n    bottomspinevisible  = false,\n    leftspinecolor      = INK_SOFT,\n    ytickcolor          = INK_SOFT,\n    xticksvisible       = false,\n    xticklabelsvisible  = false,\n    xgridvisible        = false,\n    ygridvisible        = false,\n)\n\n# Energy level lines with quantum-number labels\nfor n in levels\n    lines!(ax, [x_line_start, x_line_end], [level_y[n], level_y[n]];\n           color = INK_SOFT, linewidth = 2.6)\n    text!(ax, x_label, level_y[n]; text = \"n = $n\", color = INK,\n          fontsize = 16, align = (:left, :center))\nend\n\n# Ionization limit (n = ∞, 0 eV), dashed to tell it apart from the levels\nlines!(ax, [x_line_start, x_line_end], [ionization_y, ionization_y];\n       color = INK_MUTED, linewidth = 2.0, linestyle = :dash)\ntext!(ax, x_label, ionization_y; text = \"n = ∞ (ionized)\", color = INK_MUTED,\n      fontsize = 16, align = (:left, :center))\n\n# Emission arrows, colored by spectral series, with wavelength labels\nfor i in 1:nrow(df)\n    hi, lo = df[i, UPPER_LEVEL], df[i, LOWER_LEVEL]\n    color = IMPRINT_PALETTE[findfirst(==(df[i, SERIES]), series)]\n    y_from, y_to = level_y[hi], level_y[lo]\n    arrows2d!(ax, [arrow_x[i]], [y_from], [0.0], [y_to - y_from];\n              color = color, shaftwidth = 3.2, tipwidth = 15, tiplength = 16)\n    wavelength = HC_EV_NM / (RYDBERG_EV * (1 / lo^2 - 1 / hi^2))\n    text!(ax, arrow_x[i] + 0.02, (y_from + y_to) / 2 + 0.2;\n          text = @sprintf(\"%d nm\", round(Int, wavelength)),\n          color = color, fontsize = 15, align = (:left, :center))\nend\n\n# Series legend\nLegend(fig[1, 1],\n       [LineElement(color = IMPRINT_PALETTE[i], linewidth = 4) for i in eachindex(series)],\n       series;\n       tellwidth = false, tellheight = false,\n       halign = :center, valign = :bottom, orientation = :horizontal,\n       framevisible = false, labelcolor = INK, labelsize = 16, padding = (0, 0, 8, 8))\n\nxlims!(ax, 0.0, 1.08)\nylims!(ax, 0.3, ionization_y + 0.5)\n\nsave(\"plot-$(THEME).png\", fig; px_per_unit = 2)\n"}