{"spec_id":"heatmap-mandelbrot","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nheatmap-mandelbrot: Mandelbrot Set Fractal Visualization\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 88/100 | Updated: 2026-05-30\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom matplotlib.colors import LinearSegmentedColormap, PowerNorm\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\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\n\n# Imprint sequential colormap (single-polarity: green→blue)\ncmap = LinearSegmentedColormap.from_list(\"imprint_seq\", [\"#009E73\", \"#4467A3\"])\ncmap.set_bad(color=\"#1A1A17\")  # interior of the set stays dark on both themes\n\n# Data — compute Mandelbrot escape iterations on the complex plane\nx_min, x_max = -2.5, 1.0\ny_min, y_max = -1.25, 1.25\nwidth_px, height_px = 1200, 860\nmax_iterations = 200\n\nreal = np.linspace(x_min, x_max, width_px)\nimag = np.linspace(y_min, y_max, height_px)\nreal_grid, imag_grid = np.meshgrid(real, imag)\nc = real_grid + 1j * imag_grid\n\nz = np.zeros_like(c)\niterations = np.zeros(c.shape, dtype=float)\nmask = np.ones(c.shape, dtype=bool)\n\nfor i in range(max_iterations):\n    z[mask] = z[mask] ** 2 + c[mask]\n    escaped = mask & (np.abs(z) > 2)\n    # Smooth coloring: fractional escape count eliminates discrete banding\n    iterations[escaped] = i + 1 - np.log2(np.log2(np.abs(z[escaped])))\n    mask[escaped] = False\n\n# Points inside the set get NaN so cmap.set_bad() renders them dark\niterations[mask] = np.nan\n\n# PowerNorm(gamma=0.4) compresses the high end, revealing more boundary detail\nnorm = PowerNorm(gamma=0.4, vmin=np.nanmin(iterations), vmax=np.nanmax(iterations))\n\n# Plot — square canvas (heatmap genre)\nfig, ax = plt.subplots(figsize=(6, 6), dpi=400, facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\nimg = ax.imshow(\n    iterations,\n    extent=[x_min, x_max, y_min, y_max],\n    origin=\"lower\",\n    cmap=cmap,\n    norm=norm,\n    aspect=\"equal\",\n    interpolation=\"bilinear\",\n)\n\n# Subtle contour lines at escape boundaries add depth without cluttering the fractal\niterations_clean = np.where(np.isnan(iterations), 0, iterations)\nax.contour(\n    iterations_clean,\n    levels=[5, 15, 35, 70, 120],\n    extent=[x_min, x_max, y_min, y_max],\n    colors=INK_SOFT,\n    linewidths=0.3,\n    alpha=0.15,\n    origin=\"lower\",\n)\n\n# Colorbar\ncbar = fig.colorbar(img, ax=ax, fraction=0.03, pad=0.02)\ncbar.set_label(\"Escape Iterations\", fontsize=10, color=INK_SOFT)\ncbar.ax.tick_params(labelsize=8, colors=INK_SOFT, labelcolor=INK_SOFT)\ncbar.outline.set_edgecolor(INK_SOFT)\n\n# Style — simplified axis labels remove redundancy (Re(c) not \"Real Part Re(c)\")\ntitle = \"heatmap-mandelbrot · python · matplotlib · anyplot.ai\"\nax.set_xlabel(r\"$\\mathrm{Re}(c)$\", fontsize=10, color=INK)\nax.set_ylabel(r\"$\\mathrm{Im}(c)$\", fontsize=10, color=INK)\nax.set_title(title, fontsize=12, fontweight=\"medium\", color=INK, pad=16)\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT, labelcolor=INK_SOFT)\n\nfor spine in ax.spines.values():\n    spine.set_visible(False)\n\nfig.subplots_adjust(left=0.11, right=0.88, top=0.93, bottom=0.09)\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}