{"spec_id":"contour-3d","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\ncontour-3d: 3D Contour Plot\nLibrary: plotly 6.7.0 | Python 3.13.13\nQuality: 93/100 | Updated: 2026-05-16\n\"\"\"\n\nimport os\nimport sys\n\n\n# Prevent self-import: remove this script's directory from sys.path so that\n# 'import plotly' resolves to the installed package, not this file.\n_here = os.path.dirname(os.path.abspath(__file__))\nsys.path = [p for p in sys.path if p != \"\" and os.path.abspath(p or \".\") != _here]\n\nimport numpy as np\nimport plotly.graph_objects as go\n\n\n# Theme tokens\nTHEME = os.environ.get(\"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\"\nGRID = \"rgba(26,26,23,0.10)\" if THEME == \"light\" else \"rgba(240,239,232,0.10)\"\nGRID_DARK = \"rgba(26,26,23,0.15)\" if THEME == \"light\" else \"rgba(240,239,232,0.15)\"\n\n# Data - create a mathematical surface with interesting contour features\nn = 40  # Grid size for clear visualization\n\nx = np.linspace(-3, 3, n)\ny = np.linspace(-3, 3, n)\nX, Y = np.meshgrid(x, y)\n\n# Create a surface with peaks and valleys - good for showing contours\n# Combination of Gaussian peaks and a saddle point\nZ = (\n    1.5 * np.exp(-((X - 1) ** 2 + (Y - 1) ** 2))  # Peak at (1, 1)\n    + 1.0 * np.exp(-((X + 1) ** 2 + (Y + 1) ** 2))  # Peak at (-1, -1)\n    - 0.8 * np.exp(-((X + 1) ** 2 + (Y - 1) ** 2))  # Valley at (-1, 1)\n    + 0.3 * (X**2 - Y**2) * 0.1  # Subtle saddle point contribution\n)\n\n# Create figure with 3D contour surface\nfig = go.Figure()\n\n# Add the 3D surface with contour lines\nfig.add_trace(\n    go.Surface(\n        x=X,\n        y=Y,\n        z=Z,\n        colorscale=\"Viridis\",\n        showscale=True,\n        contours={\n            \"z\": {\n                \"show\": True,\n                \"usecolormap\": True,\n                \"highlightcolor\": INK,\n                \"project_z\": True,  # Project contours onto the base plane\n                \"width\": 3,  # More prominent contour lines\n            },\n            \"x\": {\"show\": False},\n            \"y\": {\"show\": False},\n        },\n        colorbar={\n            \"title\": {\"text\": \"Height (z)\", \"font\": {\"size\": 22, \"color\": INK}},\n            \"tickfont\": {\"size\": 18, \"color\": INK_SOFT},\n            \"len\": 0.7,\n            \"thickness\": 28,\n            \"x\": 1.02,\n            \"tickcolor\": INK_SOFT,\n            \"bordercolor\": INK_SOFT,\n            \"borderwidth\": 1,\n            \"bgcolor\": ELEVATED_BG,\n        },\n        hovertemplate=\"<b>Surface Value</b><br>X: %{x:.2f}<br>Y: %{y:.2f}<br>Z: %{z:.3f}<extra></extra>\",\n    )\n)\n\n# Update layout for large canvas and clear visualization\nfig.update_layout(\n    title={\n        \"text\": \"contour-3d · plotly · anyplot.ai\",\n        \"font\": {\"size\": 28, \"color\": INK},\n        \"x\": 0.5,\n        \"xanchor\": \"center\",\n    },\n    scene={\n        \"xaxis\": {\n            \"title\": {\"text\": \"X Coordinate\", \"font\": {\"size\": 22, \"color\": INK}},\n            \"tickfont\": {\"size\": 18, \"color\": INK_SOFT},\n            \"gridcolor\": GRID_DARK,\n            \"showbackground\": True,\n            \"backgroundcolor\": PAGE_BG,\n            \"linecolor\": INK_SOFT,\n        },\n        \"yaxis\": {\n            \"title\": {\"text\": \"Y Coordinate\", \"font\": {\"size\": 22, \"color\": INK}},\n            \"tickfont\": {\"size\": 18, \"color\": INK_SOFT},\n            \"gridcolor\": GRID_DARK,\n            \"showbackground\": True,\n            \"backgroundcolor\": PAGE_BG,\n            \"linecolor\": INK_SOFT,\n        },\n        \"zaxis\": {\n            \"title\": {\"text\": \"Height (z)\", \"font\": {\"size\": 22, \"color\": INK}},\n            \"tickfont\": {\"size\": 18, \"color\": INK_SOFT},\n            \"gridcolor\": GRID_DARK,\n            \"showbackground\": True,\n            \"backgroundcolor\": PAGE_BG,\n            \"linecolor\": INK_SOFT,\n        },\n        \"camera\": {\"eye\": {\"x\": 1.5, \"y\": 1.5, \"z\": 1.2}},  # Good viewing angle\n        \"aspectratio\": {\"x\": 1, \"y\": 1, \"z\": 0.7},\n    },\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    margin={\"l\": 20, \"r\": 100, \"t\": 80, \"b\": 20},\n    font={\"color\": INK},\n)\n\n# Save as PNG (4800 x 2700 px)\nfig.write_image(f\"plot-{THEME}.png\", width=1600, height=900, scale=3)\n\n# Save interactive HTML version\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}