{"spec_id":"histogram-2d","library":"plotnine","language":"python","code":"\"\"\" anyplot.ai\nhistogram-2d: 2D Histogram Heatmap\nLibrary: plotnine 0.15.4 | Python 3.13.13\nQuality: 93/100 | Updated: 2026-05-08\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport pandas as pd\nfrom plotnine import aes, element_line, element_rect, element_text, geom_bin2d, ggplot, labs, scale_fill_cmap, theme\n\n\n# Theme tokens\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 - Bivariate normal distribution with correlation\n# Context: Financial returns across asset classes\nnp.random.seed(42)\nn_points = 5000\nmean = [5.2, 8.1]\ncov = [[2.5, 1.8], [1.8, 4.2]]\nxy = np.random.multivariate_normal(mean, cov, n_points)\ndf = pd.DataFrame({\"asset_returns\": xy[:, 0], \"index_returns\": xy[:, 1]})\n\n# Create 2D histogram heatmap\nplot = (\n    ggplot(df, aes(x=\"asset_returns\", y=\"index_returns\"))\n    + geom_bin2d(bins=40)\n    + scale_fill_cmap(cmap_name=\"viridis\", name=\"Density\")\n    + labs(x=\"Asset Returns (%)\", y=\"Index Returns (%)\", title=\"histogram-2d · plotnine · anyplot.ai\")\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_border=element_rect(color=INK_SOFT, fill=None, size=0.5),\n        panel_grid_major=element_line(color=INK_SOFT, size=0.2, alpha=0.08),\n        panel_grid_minor=element_line(color=INK_SOFT, size=0.1, alpha=0.05),\n        axis_title=element_text(size=20, color=INK),\n        axis_text=element_text(size=16, color=INK_SOFT),\n        axis_line=element_line(color=INK_SOFT, size=0.4),\n        plot_title=element_text(size=24, color=INK),\n        legend_background=element_rect(fill=ELEVATED_BG, color=INK_SOFT, size=0.5),\n        legend_text=element_text(size=14, color=INK_SOFT),\n        legend_title=element_text(size=16, color=INK),\n        text=element_text(size=14),\n    )\n)\n\n# Save\nplot.save(f\"plot-{THEME}.png\", dpi=300, verbose=False)\n"}