{"spec_id":"subplot-grid","library":"matplotlib","language":"python","code":"\"\"\" anyplot.ai\nsubplot-grid: Subplot Grid Layout\nLibrary: matplotlib 3.10.9 | Python 3.13.13\nQuality: 73/100 | Updated: 2026-05-13\n\"\"\"\n\nimport matplotlib.pyplot as plt\nimport numpy as np\n\n\n# Data - Financial dashboard example\nnp.random.seed(42)\n\n# Time axis (trading days)\ndays = np.arange(1, 101)\n\n# Price data (random walk with drift)\nprice_changes = np.random.randn(100) * 2 + 0.05\nprices = 100 + np.cumsum(price_changes)\n\n# Volume data (lognormal distribution)\nvolumes = np.random.lognormal(mean=10, sigma=0.5, size=100)\n\n# Daily returns\nreturns = np.diff(prices) / prices[:-1] * 100\n\n# Moving averages\nma_20 = np.convolve(prices, np.ones(20) / 20, mode=\"valid\")\n\n# Create 2x2 subplot grid\nfig, axes = plt.subplots(2, 2, figsize=(16, 9))\n\n# Subplot 1: Price Line Chart (top-left)\nax1 = axes[0, 0]\nax1.plot(days, prices, linewidth=2.5, color=\"#306998\", label=\"Price\")\nax1.plot(days[19:], ma_20, linewidth=2, color=\"#FFD43B\", linestyle=\"--\", label=\"20-day MA\")\nax1.set_xlabel(\"Trading Day\", fontsize=16)\nax1.set_ylabel(\"Price ($)\", fontsize=16)\nax1.set_title(\"Stock Price\", fontsize=18, fontweight=\"bold\")\nax1.tick_params(axis=\"both\", labelsize=14)\nax1.legend(fontsize=14, loc=\"upper left\")\nax1.grid(True, alpha=0.3, linestyle=\"--\")\n\n# Subplot 2: Volume Bar Chart (top-right)\nax2 = axes[0, 1]\ncolors = [\"#306998\" if r >= 0 else \"#D94A4A\" for r in np.append(0, returns)]\nax2.bar(days, volumes / 1000, width=0.8, color=colors, alpha=0.8, edgecolor=\"none\")\nax2.set_xlabel(\"Trading Day\", fontsize=16)\nax2.set_ylabel(\"Volume (thousands)\", fontsize=16)\nax2.set_title(\"Trading Volume\", fontsize=18, fontweight=\"bold\")\nax2.tick_params(axis=\"both\", labelsize=14)\nax2.grid(True, alpha=0.3, linestyle=\"--\", axis=\"y\")\n\n# Subplot 3: Returns Histogram (bottom-left)\nax3 = axes[1, 0]\nax3.hist(returns, bins=20, color=\"#306998\", edgecolor=\"white\", linewidth=1.5, alpha=0.8)\nax3.axvline(x=0, color=\"#FFD43B\", linewidth=2.5, linestyle=\"-\", label=\"Zero Return\")\nax3.axvline(x=np.mean(returns), color=\"#D94A4A\", linewidth=2.5, linestyle=\"--\", label=f\"Mean: {np.mean(returns):.2f}%\")\nax3.set_xlabel(\"Daily Return (%)\", fontsize=16)\nax3.set_ylabel(\"Frequency\", fontsize=16)\nax3.set_title(\"Return Distribution\", fontsize=18, fontweight=\"bold\")\nax3.tick_params(axis=\"both\", labelsize=14)\nax3.legend(fontsize=12, loc=\"upper right\")\nax3.grid(True, alpha=0.3, linestyle=\"--\", axis=\"y\")\n\n# Subplot 4: Price vs Volume Scatter (bottom-right)\nax4 = axes[1, 1]\nscatter = ax4.scatter(\n    volumes[1:] / 1000,  # Match returns size (99 elements)\n    np.abs(returns),\n    s=80,\n    c=returns,\n    cmap=\"RdYlGn\",\n    alpha=0.7,\n    edgecolor=\"white\",\n    linewidth=0.5,\n)\nax4.set_xlabel(\"Volume (thousands)\", fontsize=16)\nax4.set_ylabel(\"Absolute Return (%)\", fontsize=16)\nax4.set_title(\"Volume vs Return Magnitude\", fontsize=18, fontweight=\"bold\")\nax4.tick_params(axis=\"both\", labelsize=14)\ncbar = plt.colorbar(scatter, ax=ax4)\ncbar.ax.tick_params(labelsize=12)\ncbar.set_label(\"Return (%)\", fontsize=14)\nax4.grid(True, alpha=0.3, linestyle=\"--\")\n\n# Main title\nfig.suptitle(\"subplot-grid · matplotlib · pyplots.ai\", fontsize=24, fontweight=\"bold\", y=0.98)\n\nplt.tight_layout(rect=[0, 0, 1, 0.95])\nplt.savefig(\"plot.png\", dpi=300, bbox_inches=\"tight\")\n"}