{"spec_id":"area-elevation-profile","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\narea-elevation-profile: Terrain Elevation Profile Along Transect\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 91/100 | Updated: 2026-06-10\n\"\"\"\n\nimport os\n\nimport matplotlib.gridspec as gridspec\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nfrom matplotlib.colors import LinearSegmentedColormap\n\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\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\n\n# Imprint palette — first categorical series always #009E73\nIMPRINT_PALETTE = [\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\"]\n\n# Imprint sequential cmap for continuous terrain fill and slope-coded markers\nimprint_seq = LinearSegmentedColormap.from_list(\"imprint_seq\", [\"#009E73\", \"#4467A3\"])\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        \"grid.linestyle\": \"-\",\n        \"grid.linewidth\": 0.5,\n        \"legend.facecolor\": ELEVATED_BG,\n        \"legend.edgecolor\": INK_SOFT,\n    },\n)\n\n# Data\nnp.random.seed(42)\n\ntotal_distance = 120\nn_points = 480\ndistance = np.linspace(0, total_distance, n_points)\n\nbase_profile = (\n    800\n    + 600 * np.sin(distance / total_distance * np.pi * 0.8)\n    + 400 * np.sin(distance / total_distance * np.pi * 2.5 + 0.5)\n    + 200 * np.sin(distance / total_distance * np.pi * 5 + 1.2)\n    + 150 * np.cos(distance / total_distance * np.pi * 3.8)\n)\nnoise = np.cumsum(np.random.normal(0, 2, n_points))\nnoise -= np.linspace(noise[0], noise[-1], n_points)\nelevation = base_profile + noise\nelevation = np.clip(elevation, 400, None)\n\ndf = pd.DataFrame({\"distance_km\": distance, \"elevation_m\": elevation})\n\nlandmarks = pd.DataFrame(\n    {\n        \"name\": [\"Trailhead\", \"North Peak\", \"Lake Valley\", \"Ridge Pass\", \"River Crossing\", \"Summit\", \"End Station\"],\n        \"distance_km\": [0.0, 8.0, 28.0, 52.0, 75.0, 105.0, 120.0],\n    }\n)\nlandmark_elevations = []\nfor d in landmarks[\"distance_km\"]:\n    idx = np.argmin(np.abs(distance - d))\n    landmark_elevations.append(elevation[idx])\nlandmarks[\"elevation_m\"] = landmark_elevations\n\nslopes = np.gradient(elevation, distance)\nlandmark_slopes = []\nfor d in landmarks[\"distance_km\"]:\n    idx = np.argmin(np.abs(distance - d))\n    landmark_slopes.append(abs(slopes[idx]))\nlandmarks[\"slope\"] = landmark_slopes\nslope_max = max(landmark_slopes)\n\n# Figure layout: main profile + marginal elevation KDE strip\nfig = plt.figure(figsize=(8, 4.5), dpi=400)\ngs = gridspec.GridSpec(1, 2, width_ratios=[20, 1], wspace=0.02)\nax = fig.add_subplot(gs[0])\nax_kde = fig.add_subplot(gs[1], sharey=ax)\n\n# Gradient terrain fill — Imprint sequential cmap (green→blue, low→high elevation)\nelev_min_val, elev_max_val = elevation.min(), elevation.max()\nelev_range = elev_max_val - elev_min_val\ny_min = elev_min_val - 0.05 * elev_range\nn_bands = 40\nfor i in range(n_bands):\n    band_low = elev_min_val + i / n_bands * elev_range\n    band_high = elev_min_val + (i + 1) / n_bands * elev_range\n    clipped = np.clip(elevation, band_low, band_high)\n    ax.fill_between(\n        distance, np.full_like(distance, band_low), clipped, color=imprint_seq(i / n_bands), alpha=0.8, linewidth=0\n    )\nax.fill_between(distance, y_min, np.full_like(distance, elev_min_val), color=imprint_seq(0.0), alpha=0.8, linewidth=0)\n\n# Profile line via seaborn lineplot\nsns.lineplot(data=df, x=\"distance_km\", y=\"elevation_m\", ax=ax, color=IMPRINT_PALETTE[2], linewidth=2.0, legend=False)\n\n# Landmark markers — slope intensity encoded via Imprint sequential cmap\nfor _, lm in landmarks.iterrows():\n    ax.scatter(\n        lm[\"distance_km\"],\n        lm[\"elevation_m\"],\n        color=imprint_seq(lm[\"slope\"] / slope_max),\n        s=90,\n        edgecolor=PAGE_BG,\n        linewidth=1.0,\n        zorder=5,\n    )\n\n# Landmark annotations with smart positioning\nfor _, lm in landmarks.iterrows():\n    ax.vlines(lm[\"distance_km\"], y_min, lm[\"elevation_m\"], color=INK_SOFT, linewidth=0.5, linestyle=\":\", alpha=0.5)\n    label_text = f\"{lm['name']}\\n{int(lm['elevation_m'])} m\"\n    y_offset = 35 if lm[\"elevation_m\"] < (elev_max_val - 200) else -50\n    x_offset = -28 if lm[\"distance_km\"] >= total_distance - 1 else (28 if lm[\"distance_km\"] <= 1 else 0)\n    ha = \"right\" if lm[\"distance_km\"] >= total_distance - 1 else (\"left\" if lm[\"distance_km\"] <= 1 else \"center\")\n    ax.annotate(\n        label_text,\n        xy=(lm[\"distance_km\"], lm[\"elevation_m\"]),\n        xytext=(x_offset, y_offset),\n        textcoords=\"offset points\",\n        fontsize=7,\n        fontweight=\"bold\",\n        color=INK,\n        ha=ha,\n        va=\"bottom\" if y_offset > 0 else \"top\",\n        bbox={\"boxstyle\": \"round,pad=0.25\", \"facecolor\": ELEVATED_BG, \"edgecolor\": INK_SOFT, \"alpha\": 0.9},\n        zorder=6,\n    )\n\n# Marginal KDE strip — distinctive seaborn feature showing elevation distribution\nsns.kdeplot(y=df[\"elevation_m\"], ax=ax_kde, fill=True, color=IMPRINT_PALETTE[2], alpha=0.3, linewidth=1.2)\nsns.rugplot(data=landmarks, y=\"elevation_m\", ax=ax_kde, color=IMPRINT_PALETTE[2], height=0.3, linewidth=1.5, alpha=0.7)\nax_kde.set_xlabel(\"\")\nax_kde.set_ylabel(\"\")\nax_kde.tick_params(left=False, labelleft=False, bottom=False, labelbottom=False)\nax_kde.set_facecolor(PAGE_BG)\nsns.despine(ax=ax_kde, left=True, bottom=True)\n\n# Style main axes\ntitle = \"area-elevation-profile · python · seaborn · anyplot.ai\"\nax.set_title(title, fontsize=12, fontweight=\"medium\", color=INK, pad=8)\nax.set_xlabel(\"Distance (km)\", fontsize=10, color=INK)\nax.set_ylabel(\"Elevation (m)\", fontsize=10, color=INK)\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT)\nax.yaxis.set_major_locator(plt.MultipleLocator(200))\nax.xaxis.set_major_locator(plt.MultipleLocator(20))\nsns.despine(ax=ax)\nax.yaxis.grid(True, alpha=0.15, linewidth=0.5)\nax.set_xlim(0, total_distance)\nax.set_ylim(bottom=y_min)\n\nax.text(\n    0.98,\n    0.02,\n    \"Vertical exaggeration ~10×\",\n    transform=ax.transAxes,\n    fontsize=7,\n    color=INK_MUTED,\n    ha=\"right\",\n    va=\"bottom\",\n    style=\"italic\",\n)\n\nfig.subplots_adjust(left=0.07, right=0.97, top=0.92, bottom=0.10)\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}