{"spec_id":"contour-3d","library":"altair","language":"python","code":"\"\"\" anyplot.ai\ncontour-3d: 3D Contour Plot\nLibrary: altair 6.1.0 | Python 3.13.13\nQuality: 82/100 | Created: 2026-05-16\n\"\"\"\n\nimport os\nimport sys\n\nimport numpy as np\nimport pandas as pd\n\n\n# Avoid name collision with the script filename\n_sys_path = sys.path[:]\nsys.path = [p for p in sys.path if not p.startswith(os.path.dirname(__file__))]\nimport altair as alt\n\n\nsys.path = _sys_path\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: Multi-modal Gaussian surface with peaks\nnp.random.seed(42)\nx = np.linspace(-6, 6, 50)\ny = np.linspace(-6, 6, 50)\nX, Y = np.meshgrid(x, y)\nZ = (\n    100 * np.exp(-(X**2 + Y**2) / 10)\n    + 60 * np.exp(-((X - 3.5) ** 2 + (Y - 3.5) ** 2) / 8)\n    + 40 * np.exp(-((X + 3) ** 2 + (Y + 2.5) ** 2) / 12)\n)\n\n# Prepare data for heatmap\ndata_points = []\nfor i in range(len(x)):\n    for j in range(len(y)):\n        data_points.append({\"x\": x[i], \"y\": y[j], \"z\": Z[j, i]})\ndf = pd.DataFrame(data_points)\n\n# Extract contour lines at regular intervals by finding grid points near each level\ncontour_levels = np.arange(10, 160, 20)  # Contours every 20 units\ncontour_tolerance = 8  # Within 8 units of each level\ncontour_data = []\n\nfor level in contour_levels:\n    # Find all grid points where z is close to this contour level\n    mask = np.abs(Z - level) < contour_tolerance\n    contour_points = np.argwhere(mask)\n    for row, col in contour_points:\n        contour_data.append({\"x\": x[col], \"y\": y[row], \"z_actual\": float(Z[row, col]), \"level\": float(level)})\n\ncontour_df = pd.DataFrame(contour_data) if contour_data else pd.DataFrame()\n\n# Create base heatmap with diverging colormap for better visual distinction\nheatmap = (\n    alt.Chart(df)\n    .mark_rect()\n    .encode(\n        x=alt.X(\"x:Q\", title=\"X Coordinate\", scale=alt.Scale(domain=[-6, 6])),\n        y=alt.Y(\"y:Q\", title=\"Y Coordinate\", scale=alt.Scale(domain=[-6, 6])),\n        color=alt.Color(\"z:Q\", title=\"Elevation\", scale=alt.Scale(scheme=\"brownbluegreen\")),\n        tooltip=[\"x:Q\", \"y:Q\", alt.Tooltip(\"z:Q\", format=\".1f\")],\n    )\n    .properties(width=1600, height=900)\n)\n\n# Overlay contour lines for explicit level visualization\nif len(contour_df) > 0:\n    contour_lines = (\n        alt.Chart(contour_df)\n        .mark_point(size=20, opacity=0.5)\n        .encode(\n            x=\"x:Q\",\n            y=\"y:Q\",\n            color=alt.Color(\"level:Q\", scale=alt.Scale(scheme=\"greys\"), legend=alt.Legend(title=\"Contour Levels\")),\n            tooltip=[\"level:Q\"],\n        )\n    )\n    chart_base = (heatmap + contour_lines).properties(\n        background=PAGE_BG, title=alt.Title(\"contour-3d · altair · anyplot.ai\", fontSize=28)\n    )\nelse:\n    chart_base = heatmap.properties(\n        background=PAGE_BG, title=alt.Title(\"contour-3d · altair · anyplot.ai\", fontSize=28)\n    )\n\n# Apply unified theme configuration\nchart = (\n    chart_base.configure_view(fill=PAGE_BG, stroke=INK_SOFT, strokeWidth=1)\n    .configure_axis(\n        domainColor=INK_SOFT,\n        domainWidth=2,\n        tickColor=INK_SOFT,\n        gridColor=INK,\n        gridOpacity=0.10,\n        labelColor=INK_SOFT,\n        labelFontSize=18,\n        titleColor=INK,\n        titleFontSize=22,\n    )\n    .configure_legend(\n        fillColor=ELEVATED_BG,\n        strokeColor=INK_SOFT,\n        labelColor=INK_SOFT,\n        labelFontSize=16,\n        titleColor=INK,\n        titleFontSize=18,\n        titlePadding=10,\n        labelPadding=10,\n    )\n    .configure_title(color=INK, anchor=\"middle\", offset=20)\n    .interactive()\n)\n\n# Save to script directory\nscript_dir = os.path.dirname(os.path.abspath(__file__))\nchart.save(os.path.join(script_dir, f\"plot-{THEME}.png\"), scale_factor=3.0)\nchart.save(os.path.join(script_dir, f\"plot-{THEME}.html\"))\n"}