{"spec_id":"contour-3d","library":"pygal","language":"python","code":"\"\"\" anyplot.ai\ncontour-3d: 3D Contour Plot\nLibrary: pygal 3.1.0 | Python 3.13.13\nQuality: 66/100 | Created: 2026-05-16\n\"\"\"\n\nimport os\nimport sys\n\n\n# Exclude the 'python' directory from sys.path — prevents local implementation files\n# (matplotlib.py, pygal.py, etc.) from shadowing the installed packages they implement.\nsys.path = [p for p in sys.path if not p.endswith(\"python\")]\n\nimport matplotlib.cm as cm\nimport matplotlib.colors as mcolors\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pygal\nfrom pygal.style import Style\n\n\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\n\nnp.random.seed(42)\nx = np.linspace(-3, 3, 60)\ny = np.linspace(-3, 3, 60)\nX, Y = np.meshgrid(x, y)\nZ = np.exp(-(X**2 + Y**2) / 2) + 0.3 * np.sin(2 * X) * np.cos(2 * Y)\n\n# Use matplotlib as a computational backend to extract contour paths\nfig, ax = plt.subplots()\ncontour_set = ax.contour(X, Y, Z, levels=8)\nplt.close(fig)\n\nactual_levels = contour_set.levels\nn_levels = len(actual_levels)\n\n# Sequential viridis palette: dark purple (low z) → yellow (high z)\nviridis_colors = tuple(mcolors.to_hex(cm.viridis(i / max(n_levels - 1, 1))) for i in range(n_levels))\n\ncustom_style = Style(\n    background=PAGE_BG,\n    plot_background=PAGE_BG,\n    foreground=INK,\n    foreground_strong=INK,\n    foreground_subtle=INK_MUTED,\n    colors=viridis_colors,\n    title_font_size=36,\n    label_font_size=28,\n    major_label_font_size=24,\n    legend_font_size=20,\n    stroke_width=4,\n)\n\nchart = pygal.XY(\n    width=4800,\n    height=2700,\n    title=\"contour-3d · pygal · anyplot.ai\",\n    x_title=\"x\",\n    y_title=\"y\",\n    style=custom_style,\n    show_legend=True,\n    show_dots=False,\n    show_x_guides=False,\n    show_y_guides=False,\n)\n\nfor z_level, segs in zip(actual_levels, contour_set.allsegs, strict=False):\n    label = f\"z = {z_level:.2f}\"\n    valid_segs = [seg for seg in segs if len(seg) > 1]\n    if not valid_segs:\n        continue\n    # Use only the longest segment per level to avoid jump-line artifacts\n    main_seg = max(valid_segs, key=len)\n    points = [{\"value\": (float(p[0]), float(p[1])), \"label\": label} for p in main_seg]\n    chart.add(label, points, show_dots=False)\n\nchart.render_to_file(f\"plot-{THEME}.svg\")\nchart.render_to_png(f\"plot-{THEME}.png\")\n\nwith open(f\"plot-{THEME}.html\", \"wb\") as f:\n    f.write(chart.render())\n"}