{"spec_id":"map-projections","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nmap-projections: World Map with Different Projections\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 84/100 | Updated: 2026-05-23\n\"\"\"\n\nimport os\nimport sys\n\n\n# Remove this script's directory from sys.path to avoid shadowing the matplotlib package\n_script_dir = os.path.dirname(os.path.abspath(__file__))\nsys.path = [p for p in sys.path if p != \"\" and os.path.abspath(p) != _script_dir]\ndel _script_dir\n\nimport cartopy.crs as ccrs\nimport cartopy.feature as cfeature\nimport matplotlib.pyplot as plt\nimport numpy as np\n\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\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# Map colors (theme-adaptive; semantic: land/ocean are not data series)\nLAND_COLOR = \"#E8E4D9\" if THEME == \"light\" else \"#3A3830\"\nOCEAN_COLOR = \"#A8D5E5\" if THEME == \"light\" else \"#1A3348\"\nTISSOT_COLOR = \"#AE3030\"  # Imprint palette position 3 (red)\n\n# Projections to compare\nprojections = [\n    (\"Mercator\", ccrs.Mercator()),\n    (\"Robinson\", ccrs.Robinson()),\n    (\"Mollweide\", ccrs.Mollweide()),\n    (\"Orthographic\", ccrs.Orthographic(central_longitude=0, central_latitude=30)),\n]\n\n# Figure — 3200×1800 px (16:9)\nfig = plt.figure(figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\nfig.suptitle(\"map-projections · python · matplotlib · anyplot.ai\", fontsize=12, fontweight=\"medium\", color=INK, y=0.98)\n\nfor i, (name, proj) in enumerate(projections):\n    ax = fig.add_subplot(2, 2, i + 1, projection=proj)\n\n    try:\n        ax.set_global()\n    except Exception:\n        pass\n\n    # Map features\n    ax.add_feature(cfeature.OCEAN, facecolor=OCEAN_COLOR, zorder=0)\n    ax.add_feature(cfeature.LAND, facecolor=LAND_COLOR, zorder=1)\n    ax.add_feature(cfeature.COASTLINE, linewidth=0.6, edgecolor=INK_SOFT, zorder=2)\n    ax.add_feature(cfeature.BORDERS, linewidth=0.3, edgecolor=INK_SOFT, alpha=0.5, zorder=2)\n\n    # Graticule with lat/lon labels where supported\n    # Disable bottom labels for top-row subplots to prevent overlap with second-row titles\n    is_top_row = i < 2\n    try:\n        gl = ax.gridlines(draw_labels=True, linewidth=0.5, color=INK_SOFT, alpha=0.4, linestyle=\"--\")\n        gl.top_labels = False\n        gl.right_labels = False\n        gl.bottom_labels = not is_top_row\n        gl.xlabel_style = {\"size\": 7, \"color\": INK_MUTED}\n        gl.ylabel_style = {\"size\": 7, \"color\": INK_MUTED}\n    except Exception:\n        gl = ax.gridlines(draw_labels=False, linewidth=0.5, color=INK_SOFT, alpha=0.4, linestyle=\"--\")\n    gl.xlocator = plt.FixedLocator(np.arange(-180, 181, 30))\n    gl.ylocator = plt.FixedLocator(np.arange(-90, 91, 30))\n\n    # Tissot indicatrices — visualise projection distortion of area/shape\n    for lon in np.arange(-150, 180, 60):\n        for lat in np.arange(-60, 90, 30):\n            try:\n                ax.tissot(\n                    rad_km=500,\n                    lons=lon,\n                    lats=lat,\n                    n_samples=64,\n                    facecolor=TISSOT_COLOR,\n                    edgecolor=\"#7B1117\",\n                    alpha=0.45 if THEME == \"dark\" else 0.35,\n                    linewidth=0.8,\n                    zorder=3,\n                )\n            except Exception:\n                pass\n\n    ax.set_title(name, fontsize=10, fontweight=\"bold\", color=INK, pad=6)\n\nfig.subplots_adjust(left=0.02, right=0.98, top=0.86, bottom=0.03, hspace=0.30, wspace=0.04)\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}