{"spec_id":"contour-basic","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\ncontour-basic: Basic Contour Plot\nLibrary: seaborn 0.13.2 | Python 3.13.14\nQuality: 89/100 | Updated: 2026-06-25\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nfrom matplotlib.colors import LinearSegmentedColormap\nfrom scipy.stats import gaussian_kde\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\"\n\n# Imprint sequential colormap for continuous density data\nimprint_seq = LinearSegmentedColormap.from_list(\"imprint_seq\", [\"#009E73\", \"#4467A3\"])\n\n# Data — bivariate distribution of weather-station readings across two synoptic regimes\nnp.random.seed(42)\ncold_front = np.random.multivariate_normal(mean=[4.8, 1021.5], cov=[[3.2, -1.3], [-1.3, 7.8]], size=1500)\nwarm_front = np.random.multivariate_normal(mean=[11.6, 1013.2], cov=[[6.0, 2.0], [2.0, 5.0]], size=900)\nreadings = pd.DataFrame(np.vstack([cold_front, warm_front]), columns=[\"Wind Speed (m/s)\", \"Barometric Pressure (hPa)\"])\n\n# Theme\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        \"axes.linewidth\": 0.9,\n        \"axes.axisbelow\": True,\n    },\n)\n\nfig, ax = plt.subplots(figsize=(8, 4.5), dpi=400)\nfig.set_facecolor(PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Filled KDE contours — seaborn's kdeplot with integrated colorbar (Imprint sequential cmap)\nsns.kdeplot(\n    data=readings,\n    x=\"Wind Speed (m/s)\",\n    y=\"Barometric Pressure (hPa)\",\n    fill=True,\n    cmap=imprint_seq,\n    thresh=0.02,\n    levels=12,\n    cbar=True,\n    cbar_kws={\"shrink\": 0.85, \"pad\": 0.02, \"label\": \"Reading Density\"},\n    ax=ax,\n)\n\n# Isoline overlay via scipy KDE grid — returns ContourSet needed for ax.clabel\nx_vals = readings[\"Wind Speed (m/s)\"].values\ny_vals = readings[\"Barometric Pressure (hPa)\"].values\nkde = gaussian_kde(np.vstack([x_vals, y_vals]))\nx_grid = np.linspace(x_vals.min() - 1, x_vals.max() + 1, 80)\ny_grid = np.linspace(y_vals.min() - 2, y_vals.max() + 2, 80)\nXX, YY = np.meshgrid(x_grid, y_grid)\nZZ = kde(np.vstack([XX.ravel(), YY.ravel()])).reshape(XX.shape)\ncs = ax.contour(XX, YY, ZZ, levels=10, colors=INK, alpha=0.35, linewidths=0.7)\nax.clabel(cs, levels=cs.levels[2::3], inline=True, fontsize=8, fmt=\"%.3f\", colors=INK)\n\n# Subtle reference line separating the two regimes\nax.axhline(1017, color=INK_SOFT, linewidth=0.8, linestyle=\":\", alpha=0.5)\n\n# Mode annotations to guide the viewer\nax.text(4.8, 1020.2, \"Cold Front\", fontsize=8, color=INK_SOFT, ha=\"center\", style=\"italic\")\nax.text(11.6, 1014.5, \"Warm Front\", fontsize=8, color=INK_SOFT, ha=\"center\", style=\"italic\")\n\n# Subtle grid for value reading aid\nax.yaxis.grid(True, color=INK, alpha=0.12, linewidth=0.6, linestyle=\"--\")\nax.set_axisbelow(True)\n\n# Style\ntitle = \"contour-basic · python · seaborn · anyplot.ai\"\nax.set_title(title, fontsize=12, fontweight=\"medium\", color=INK, pad=14)\nax.set_xlabel(\"Wind Speed (m/s)\", fontsize=10, color=INK, labelpad=10)\nax.set_ylabel(\"Barometric Pressure (hPa)\", fontsize=10, color=INK, labelpad=10)\nax.tick_params(axis=\"both\", labelsize=8, colors=INK_SOFT, length=0)\nsns.despine(ax=ax)\n\n# Colorbar chrome (theme-adaptive)\ncbar_ax = fig.axes[-1]\ncbar_ax.tick_params(labelsize=8, colors=INK_SOFT, length=0)\ncbar_ax.yaxis.label.set_color(INK)\ncbar_ax.yaxis.label.set_fontsize(9)\ncbar_ax.set_facecolor(ELEVATED_BG)\nfor spine in cbar_ax.spines.values():\n    spine.set_color(INK_SOFT)\n    spine.set_linewidth(0.8)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}