{"spec_id":"contour-density","library":"plotnine","language":"python","code":"\"\"\" anyplot.ai\ncontour-density: Density Contour Plot\nLibrary: plotnine 0.15.4 | Python 3.13.13\nQuality: 95/100 | Updated: 2026-05-16\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport pandas as pd\nfrom plotnine import (\n    aes,\n    element_line,\n    element_rect,\n    element_text,\n    geom_density_2d,\n    geom_point,\n    ggplot,\n    labs,\n    stat_density_2d,\n    theme,\n    theme_minimal,\n)\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\"\n\n# Data - create bivariate distribution with clusters\nnp.random.seed(42)\n\n# Main cluster\nn1 = 300\nx1 = np.random.normal(50, 8, n1)\ny1 = np.random.normal(45, 10, n1)\n\n# Secondary cluster\nn2 = 200\nx2 = np.random.normal(75, 6, n2)\ny2 = np.random.normal(70, 7, n2)\n\n# Smaller outlying cluster\nn3 = 100\nx3 = np.random.normal(30, 5, n3)\ny3 = np.random.normal(75, 5, n3)\n\n# Combine all data\nx = np.concatenate([x1, x2, x3])\ny = np.concatenate([y1, y2, y3])\n\ndf = pd.DataFrame({\"Temperature (°C)\": x, \"Pressure (kPa)\": y})\n\n# Plot\nplot = (\n    ggplot(df, aes(x=\"Temperature (°C)\", y=\"Pressure (kPa)\"))\n    + geom_density_2d(stat=stat_density_2d(levels=12), color=\"#009E73\", size=1.5)\n    + geom_point(alpha=0.15, size=2.5, color=INK_SOFT)\n    + labs(x=\"Temperature (°C)\", y=\"Pressure (kPa)\", title=\"contour-density · plotnine · anyplot.ai\")\n    + theme_minimal()\n    + theme(\n        figure_size=(16, 9),\n        plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n        panel_grid_major=element_line(color=INK, size=0.3, alpha=0.10),\n        panel_grid_minor=element_line(color=INK, size=0.2, alpha=0.05),\n        panel_border=element_rect(color=INK_SOFT, fill=None),\n        axis_title=element_text(size=20, color=INK),\n        axis_text=element_text(size=16, color=INK_SOFT),\n        plot_title=element_text(size=24, color=INK),\n        legend_position=\"none\",\n    )\n)\n\n# Save\nplot.save(f\"plot-{THEME}.png\", dpi=300, verbose=False)\n"}