{"spec_id":"contour-density","library":"letsplot","language":"python","code":"\"\"\" anyplot.ai\ncontour-density: Density Contour Plot\nLibrary: letsplot 4.9.0 | Python 3.13.13\nQuality: 90/100 | Updated: 2026-05-16\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport pandas as pd\nfrom lets_plot import *\n\n\nLetsPlot.setup_html()\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\"\nRULE = \"rgba(26,26,23,0.10)\" if THEME == \"light\" else \"rgba(240,239,232,0.10)\"\nBRAND = \"#009E73\"\n\n# Data - three clusters with different spreads for bivariate distribution\nnp.random.seed(42)\n\n# Cluster 1: Dense core cluster (temperature measurements)\nn1 = 400\nx1 = np.random.normal(loc=22, scale=3, size=n1)\ny1 = np.random.normal(loc=55, scale=8, size=n1)\n\n# Cluster 2: More diffuse cluster\nn2 = 300\nx2 = np.random.normal(loc=32, scale=5, size=n2)\ny2 = np.random.normal(loc=75, scale=10, size=n2)\n\n# Cluster 3: Small dense cluster\nn3 = 150\nx3 = np.random.normal(loc=15, scale=2, size=n3)\ny3 = np.random.normal(loc=80, scale=5, size=n3)\n\n# Combine clusters\nx = np.concatenate([x1, x2, x3])\ny = np.concatenate([y1, y2, y3])\n\ndf = pd.DataFrame({\"temperature\": x, \"humidity\": y})\n\n# Plot - density contour with scatter overlay for context\nanyplot_theme = theme(\n    plot_background=element_rect(fill=PAGE_BG, color=PAGE_BG),\n    panel_background=element_rect(fill=PAGE_BG),\n    panel_grid_major=element_line(color=RULE, size=0.3),\n    panel_grid_minor=element_blank(),\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    axis_line=element_line(color=INK_SOFT, size=0.5),\n)\n\nplot = (\n    ggplot(df, aes(x=\"temperature\", y=\"humidity\"))\n    + geom_density2d(color=BRAND, size=1.2, bins=10)\n    + geom_point(color=BRAND, alpha=0.2, size=2)\n    + labs(x=\"Temperature (°C)\", y=\"Relative Humidity (%)\", title=\"contour-density · letsplot · anyplot.ai\")\n    + theme_minimal()\n    + anyplot_theme\n    + ggsize(1600, 900)\n)\n\n# Save as PNG (scale 3x for 4800 × 2700 px)\nggsave(plot, f\"plot-{THEME}.png\", scale=3, path=\".\")\n\n# Save as HTML for interactive version\nggsave(plot, f\"plot-{THEME}.html\", path=\".\")\n"}