{"spec_id":"contour-3d","library":"letsplot","language":"python","code":"\"\"\" anyplot.ai\ncontour-3d: 3D Contour Plot\nLibrary: letsplot 4.9.0 | Python 3.13.13\nQuality: 83/100 | Created: 2026-05-16\n\"\"\"\n\nimport os\nimport shutil\nfrom pathlib import Path\n\nimport numpy as np\nimport pandas as pd\nfrom lets_plot import *\n\n\nLetsPlot.setup_html()\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\"\n\n# Data - mathematical surface with interesting contour features\nn = 45  # Grid size for clear visualization\nx = np.linspace(-3, 3, n)\ny = np.linspace(-3, 3, n)\nX, Y = np.meshgrid(x, y)\n\n# Create surface with multiple peaks and valleys\nZ = (\n    1.5 * np.exp(-((X - 1) ** 2 + (Y - 1) ** 2))\n    + 1.0 * np.exp(-((X + 1) ** 2 + (Y + 1) ** 2))\n    - 0.8 * np.exp(-((X + 1) ** 2 + (Y - 1) ** 2))\n    + 0.3 * (X**2 - Y**2) * 0.1\n)\n\n# Prepare data for letsplot\ndata = []\nfor i in range(len(x)):\n    for j in range(len(y)):\n        data.append({\"x\": X[j, i], \"y\": Y[j, i], \"z\": Z[j, i]})\ndf = pd.DataFrame(data)\n\n# Create contour-style visualization using tile with fine grid\nplot = (\n    ggplot(df, aes(\"x\", \"y\", fill=\"z\"))\n    + geom_tile(width=0.15, height=0.15)\n    + scale_fill_viridis()\n    + labs(x=\"X Coordinate\", y=\"Y Coordinate\", fill=\"Height (z)\", title=\"contour-3d · letsplot · anyplot.ai\")\n    + ggsize(1600, 900)\n    + theme(\n        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_background=element_rect(fill=PAGE_BG),\n        panel_grid_major=element_line(color=INK_SOFT, size=0.2),\n        panel_grid_minor=element_blank(),\n        axis_title=element_text(color=INK, size=20),\n        axis_text=element_text(color=INK_SOFT, size=16),\n        plot_title=element_text(color=INK, size=24),\n        legend_background=element_rect(fill=ELEVATED_BG, color=INK_SOFT),\n        legend_text=element_text(color=INK_SOFT, size=16),\n        legend_title=element_text(color=INK, size=16),\n    )\n)\n\nggsave(plot, f\"plot-{THEME}.png\", scale=3)\nggsave(plot, f\"plot-{THEME}.html\")\n\n# letsplot creates files in lets-plot-images/ subdirectory; move them to root\nfor ext in [\".png\", \".html\"]:\n    src = Path(f\"lets-plot-images/plot-{THEME}{ext}\")\n    dst = Path(f\"plot-{THEME}{ext}\")\n    if src.exists():\n        shutil.move(str(src), str(dst))\n"}