{"spec_id":"contour-3d","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\ncontour-3d: 3D Contour Plot\nLibrary: seaborn 0.13.2 | Python 3.13.15\nQuality: 87/100 | Updated: 2026-08-25\n\"\"\"\n\nimport os\nimport sys\n\n\n# Remove script directory temporarily — local matplotlib.py would shadow the package\n_here = sys.path.pop(0)\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport seaborn as sns\nfrom matplotlib.colors import TwoSlopeNorm, to_rgba\n\n\nsys.path.insert(0, _here)\n\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\nsns.set_theme(\n    style=\"ticks\",\n    rc={\n        \"figure.facecolor\": PAGE_BG,\n        \"axes.facecolor\": PAGE_BG,\n        \"axes.edgecolor\": INK_SOFT,\n        \"axes.labelcolor\": INK,\n        \"text.color\": INK,\n        \"xtick.color\": INK_SOFT,\n        \"ytick.color\": INK_SOFT,\n        \"grid.color\": INK,\n        \"grid.alpha\": 0.15,\n        \"legend.facecolor\": ELEVATED_BG,\n        \"legend.edgecolor\": INK_SOFT,\n    },\n)\n\n# Data — vibration displacement on a plate (physical science context)\nx = np.linspace(-5, 5, 40)\ny = np.linspace(-5, 5, 40)\nX, Y = np.meshgrid(x, y)\nZ_raw = np.exp(-(X**2 + Y**2) / 5) * np.cos(X) * np.sin(Y)\nZ = Z_raw * 10  # scale to micrometers\n\n# Zero displacement (equilibrium) is a meaningful midpoint, so a diverging\n# Imprint colormap maps naturally onto \"above rest\" vs \"below rest\".\nmidpoint = PAGE_BG\nimprint_div = sns.blend_palette([\"#AE3030\", midpoint, \"#4467A3\"], as_cmap=True)\nnorm = TwoSlopeNorm(vmin=Z.min(), vcenter=0, vmax=Z.max())\n\n# Plot — square canvas suits a cube-shaped 3D box better than 16:9 (avoids\n# the dead space a landscape figure leaves beside a centered 3D axes box)\nfig = plt.figure(figsize=(6, 6), dpi=400, facecolor=PAGE_BG)\nax = fig.add_subplot(111, projection=\"3d\")\nax.set_facecolor(PAGE_BG)\nax.set_box_aspect((1, 1, 0.8), zoom=1.12)\n\n# 3D surface\nsurf = ax.plot_surface(\n    X, Y, Z, cmap=imprint_div, norm=norm, alpha=0.85, edgecolor=\"none\", linewidth=0, antialiased=True\n)\n\n# On-surface contour lines — lifted a hair above the surface (z-fighting\n# otherwise hides segments behind surface polygons), bold ink color, fully\n# opaque so they read clearly against the semi-transparent diverging fill\ncontour_lift = (Z.max() - Z.min()) * 0.006\nax.contour(X, Y, Z + contour_lift, levels=12, colors=INK, alpha=1.0, linewidths=2.5)\n\n# Projected contours onto base plane — same colormap/norm as the surface for reference\nz_min = Z.min() - 0.8\nax.contour(X, Y, Z, levels=12, zdir=\"z\", offset=z_min, cmap=imprint_div, norm=norm, alpha=0.7, linewidths=1.6)\n\n# Colorbar\ncbar = fig.colorbar(surf, ax=ax, pad=0.06, fraction=0.035, aspect=28)\ncbar.set_label(\"Displacement (μm)\", fontsize=10, color=INK, labelpad=10)\ncbar.ax.tick_params(labelsize=8, colors=INK_SOFT)\ncbar.outline.set_edgecolor(INK_SOFT)\n\n# Axis labels with physical units\nax.set_xlabel(\"x Position (cm)\", fontsize=10, color=INK, labelpad=8)\nax.set_ylabel(\"y Position (cm)\", fontsize=10, color=INK, labelpad=8)\nax.set_zlabel(\"Displacement (μm)\", fontsize=10, color=INK, labelpad=8)\nfig.suptitle(\"contour-3d · python · seaborn · anyplot.ai\", fontsize=12, fontweight=\"medium\", color=INK, y=0.97)\n\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT)\nax.xaxis.label.set_color(INK)\nax.yaxis.label.set_color(INK)\nax.zaxis.label.set_color(INK)\nax.grid(True, alpha=0.15, color=INK_SOFT, linewidth=0.8)\n\nax.view_init(elev=25, azim=45)\n\n# Panes — edges only, no fill\nax.xaxis.pane.fill = False\nax.yaxis.pane.fill = False\nax.zaxis.pane.fill = False\nax.xaxis.pane.set_edgecolor(INK_SOFT)\nax.yaxis.pane.set_edgecolor(INK_SOFT)\nax.zaxis.pane.set_edgecolor(INK_SOFT)\nax.xaxis.pane.set_alpha(0.1)\nax.yaxis.pane.set_alpha(0.1)\nax.zaxis.pane.set_alpha(0.1)\n\n# mplot3d doesn't fully honor ax.grid(alpha=...) for pane grid lines —\n# restyle the internal grid color/linewidth directly so the box reads as\n# subtle theme chrome instead of bold high-contrast lines\nsubtle_grid = to_rgba(INK_SOFT, alpha=0.15)\nfor pane_axis in (ax.xaxis, ax.yaxis, ax.zaxis):\n    pane_axis._axinfo[\"grid\"].update(color=subtle_grid, linewidth=0.5)\n    pane_axis.pane.set_linewidth(0.5)\n\n# Peak annotation — identify and label the maximum displacement point\npeak_idx = np.unravel_index(np.argmax(Z), Z.shape)\npeak_x, peak_y, peak_z = X[peak_idx], Y[peak_idx], Z[peak_idx]\nax.text(\n    peak_x,\n    peak_y,\n    peak_z + 0.6,\n    f\"Peak: {peak_z:.1f} μm\",\n    fontsize=8,\n    color=INK,\n    bbox={\"facecolor\": ELEVATED_BG, \"edgecolor\": INK_SOFT, \"alpha\": 0.9, \"boxstyle\": \"round,pad=0.3\"},\n    ha=\"center\",\n)\n\nfig.subplots_adjust(left=0.06, right=0.90, top=0.90, bottom=0.06)\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}