{"spec_id":"scatter-color-mapped","library":"letsplot","language":"python","code":"\"\"\" anyplot.ai\nscatter-color-mapped: Color-Mapped Scatter Plot\nLibrary: letsplot 4.9.0 | Python 3.13.13\nQuality: 91/100 | Updated: 2026-05-08\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport pandas as pd\nfrom lets_plot import *\nfrom lets_plot import element_line, element_rect, element_text, theme\nfrom lets_plot.export import ggsave as export_ggsave\n\n\nLetsPlot.setup_html()\n\n# Theme tokens (see prompts/default-style-guide.md \"Theme-adaptive Chrome\")\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# Data - Simulating temperature readings across a spatial grid\nnp.random.seed(42)\nn_points = 150\n\n# Create spatial coordinates with some clustering\nx = np.concatenate(\n    [\n        np.random.normal(20, 8, n_points // 3),\n        np.random.normal(50, 10, n_points // 3),\n        np.random.normal(75, 6, n_points - 2 * (n_points // 3)),\n    ]\n)\ny = np.concatenate(\n    [\n        np.random.normal(30, 10, n_points // 3),\n        np.random.normal(60, 12, n_points // 3),\n        np.random.normal(40, 8, n_points - 2 * (n_points // 3)),\n    ]\n)\n\n# Temperature as third variable - correlated with position\ntemperature = 15 + 0.2 * x + 0.15 * y + np.random.normal(0, 3, n_points)\n\ndf = pd.DataFrame({\"x\": x, \"y\": y, \"temperature\": temperature})\n\n# Create the color-mapped scatter plot\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=INK_SOFT, size=0.3, linetype=\"solid\"),\n    panel_grid_minor=element_blank(),\n    axis_title=element_text(color=INK, size=20),\n    axis_text=element_text(color=INK_SOFT, size=16),\n    axis_line=element_line(color=INK_SOFT, size=0.5),\n    plot_title=element_text(color=INK, size=24),\n    legend_background=element_rect(fill=ELEVATED_BG, color=INK_SOFT),\n    legend_text=element_text(color=INK_SOFT, size=16),\n    legend_title=element_text(color=INK, size=18),\n)\n\nplot = (\n    ggplot(df, aes(x=\"x\", y=\"y\", color=\"temperature\"))\n    + geom_point(\n        size=6.5,\n        alpha=0.8,\n        tooltips=layer_tooltips()\n        .line(\"X Coord|@x\")\n        .line(\"Y Coord|@y\")\n        .line(\"Temperature|@temperature\"),\n    )\n    + scale_color_viridis(name=\"Temperature (°C)\")\n    + labs(\n        x=\"X Coordinate (m)\", y=\"Y Coordinate (m)\", title=\"scatter-color-mapped · letsplot · anyplot.ai\"\n    )\n    + theme_minimal()\n    + anyplot_theme\n    + ggsize(1600, 900)\n)\n\n# Save PNG (scale 3x to get 4800 × 2700 px)\nexport_ggsave(plot, filename=f\"plot-{THEME}.png\", path=\".\", scale=3)\n\n# Save HTML for interactive version\nexport_ggsave(plot, filename=f\"plot-{THEME}.html\", path=\".\")\n"}