{"spec_id":"contour-basic","library":"plotnine","language":"python","code":"\"\"\" anyplot.ai\ncontour-basic: Basic Contour Plot\nLibrary: plotnine 0.15.7 | Python 3.13.14\nQuality: 86/100 | Created: 2026-06-25\n\"\"\"\n\nimport os\nimport sys\n\n\n# Prevent this file from shadowing the installed plotnine package\n_here = os.path.dirname(os.path.abspath(__file__))\nif _here in sys.path:\n    sys.path.remove(_here)\nif \"\" in sys.path:\n    sys.path.remove(\"\")\n\nimport contourpy\nimport numpy as np\nimport pandas as pd\nfrom plotnine import (\n    aes,\n    element_blank,\n    element_line,\n    element_rect,\n    element_text,\n    geom_path,\n    geom_raster,\n    geom_text,\n    ggplot,\n    labs,\n    scale_fill_gradient,\n    scale_x_continuous,\n    scale_y_continuous,\n    theme,\n    theme_minimal,\n)\n\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\n# Atmospheric pressure field: two-cell anticyclone system over a regional domain\nnp.random.seed(42)\nlon = np.linspace(-10, 10, 150)\nlat = np.linspace(-8, 8, 150)\nLon, Lat = np.meshgrid(lon, lat)\n\npressure = (\n    1013.0\n    + 12.0 * np.exp(-((Lon - 3) ** 2 + (Lat - 2) ** 2) / 8)\n    + 7.0 * np.exp(-((Lon + 5) ** 2 + (Lat + 3) ** 2) / 10)\n    - 5.0 * np.exp(-((Lon - 1) ** 2 + (Lat + 5) ** 2) / 4)\n)\n\nraster_df = pd.DataFrame({\"lon\": Lon.ravel(), \"lat\": Lat.ravel(), \"hpa\": pressure.ravel()})\n\n# Extract isoline segments via contourpy (plotnine 0.15 lacks geom_contour)\np_levels = np.linspace(pressure.min(), pressure.max(), 12)\ngenerator = contourpy.contour_generator(x=Lon, y=Lat, z=pressure)\nsegments = [\n    (np.asarray(seg), gid)\n    for gid, seg in enumerate(s for lvl in p_levels for s in generator.lines(lvl))\n    if np.asarray(seg).shape[0] >= 2\n]\nline_df = pd.concat(\n    [pd.DataFrame({\"lon\": seg[:, 0], \"lat\": seg[:, 1], \"group\": gid}) for seg, gid in segments], ignore_index=True\n)\n\n# Pressure-centre labels: primary high (lon=3, lat=2), secondary high (lon=-5, lat=-3),\n# and low-pressure trough (lon=1, lat=-5)\nlabel_df = pd.DataFrame({\"lon\": [3.0, -5.0, 1.0], \"lat\": [2.5, -3.0, -5.5], \"label\": [\"H\", \"H\", \"L\"]})\n\nanyplot_theme = theme(\n    figure_size=(8, 4.5),\n    plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n    panel_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n    panel_border=element_blank(),\n    axis_line=element_blank(),\n    panel_grid_major=element_line(color=INK, size=0.3, alpha=0.1),\n    panel_grid_minor=element_line(color=INK, size=0.2, alpha=0.05),\n    axis_title=element_text(color=INK, size=10),\n    axis_text=element_text(color=INK_SOFT, size=8),\n    plot_title=element_text(color=INK, size=12),\n    legend_background=element_rect(fill=ELEVATED_BG, color=INK_SOFT),\n    legend_text=element_text(color=INK_SOFT, size=8),\n    legend_title=element_text(color=INK, size=10),\n    legend_key=element_rect(fill=PAGE_BG, color=PAGE_BG),\n)\n\nplot = (\n    ggplot()\n    + geom_raster(raster_df, aes(x=\"lon\", y=\"lat\", fill=\"hpa\"))\n    + geom_path(line_df, aes(x=\"lon\", y=\"lat\", group=\"group\"), color=\"#FFFDF6\", size=0.5, alpha=0.65)\n    + geom_text(label_df, aes(x=\"lon\", y=\"lat\", label=\"label\"), color=INK, size=5)\n    + scale_fill_gradient(low=\"#009E73\", high=\"#4467A3\", name=\"Pressure\\n(hPa)\")\n    + scale_x_continuous(expand=(0, 0))\n    + scale_y_continuous(expand=(0, 0))\n    + labs(x=\"Longitude (°E)\", y=\"Latitude (°N)\", title=\"contour-basic · python · plotnine · anyplot.ai\")\n    + theme_minimal()\n    + anyplot_theme\n)\n\nplot.save(f\"plot-{THEME}.png\", dpi=400, width=8, height=4.5, units=\"in\")\n"}