{"spec_id":"dashboard-metrics-tiles","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\ndashboard-metrics-tiles: Real-Time Dashboard Tiles\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 88/100 | Updated: 2026-05-21\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\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\"\nTILE_BORDER = \"#D5D4CD\" if THEME == \"light\" else \"#3A3936\"\n\nsns.set_theme(\n    style=\"ticks\",\n    rc={\n        \"figure.facecolor\": PAGE_BG,\n        \"axes.facecolor\": PAGE_BG,\n        \"axes.edgecolor\": INK_SOFT,\n        \"axes.labelcolor\": INK,\n        \"text.color\": INK,\n        \"xtick.color\": INK_SOFT,\n        \"ytick.color\": INK_SOFT,\n        \"grid.color\": INK,\n        \"grid.alpha\": 0.10,\n        \"legend.facecolor\": ELEVATED_BG,\n        \"legend.edgecolor\": INK_SOFT,\n    },\n)\n\n# Data\nnp.random.seed(42)\n\n# Status colors — imprint semantic anchors (green / amber / red)\nstatus_colors = {\"good\": \"#009E73\", \"warning\": \"#DDCC77\", \"critical\": \"#AE3030\"}\n\nmetrics = [\n    {\n        \"name\": \"CPU Usage\",\n        \"value\": 45,\n        \"unit\": \"%\",\n        \"history\": np.cumsum(np.random.randn(30)) + 50,\n        \"change\": -5.2,\n        \"status\": \"good\",\n    },\n    {\n        \"name\": \"Memory\",\n        \"value\": 72,\n        \"unit\": \"%\",\n        \"history\": np.cumsum(np.random.randn(30)) + 70,\n        \"change\": 8.1,\n        \"status\": \"warning\",\n    },\n    {\n        \"name\": \"Response Time\",\n        \"value\": 120,\n        \"unit\": \"ms\",\n        \"history\": np.cumsum(np.random.randn(30)) + 130,\n        \"change\": -15.3,\n        \"status\": \"good\",\n    },\n    {\n        \"name\": \"Active Users\",\n        \"value\": 1847,\n        \"unit\": \"\",\n        \"history\": np.cumsum(np.random.randn(30)) * 50 + 1800,\n        \"change\": 12.7,\n        \"status\": \"good\",\n    },\n    {\n        \"name\": \"Error Rate\",\n        \"value\": 2.3,\n        \"unit\": \"%\",\n        \"history\": np.cumsum(np.random.randn(30)) * 0.5 + 2,\n        \"change\": 45.0,\n        \"status\": \"critical\",\n    },\n    {\n        \"name\": \"Throughput\",\n        \"value\": 892,\n        \"unit\": \"req/s\",\n        \"history\": np.cumsum(np.random.randn(30)) * 30 + 850,\n        \"change\": -3.4,\n        \"status\": \"good\",\n    },\n]\n\n# Plot — canonical landscape canvas: figsize=(8, 4.5) @ dpi=400 → 3200×1800 px\nfig, axes = plt.subplots(2, 3, figsize=(8, 4.5), dpi=400, facecolor=PAGE_BG)\naxes = axes.flatten()\n\nfor ax, metric in zip(axes, metrics, strict=True):\n    ax.set_facecolor(ELEVATED_BG)\n    for spine in ax.spines.values():\n        spine.set_color(TILE_BORDER)\n        spine.set_linewidth(1.5)\n\n    # Sparkline inset — seaborn lineplot in bottom quarter of tile\n    inset_ax = ax.inset_axes([0.1, 0.08, 0.8, 0.25])\n    inset_ax.set_facecolor(ELEVATED_BG)\n\n    history = metric[\"history\"]\n    spark_df = pd.DataFrame({\"time\": np.arange(len(history)), \"value\": history})\n\n    sns.lineplot(data=spark_df, x=\"time\", y=\"value\", ax=inset_ax, color=status_colors[metric[\"status\"]], linewidth=1.5)\n    inset_ax.fill_between(\n        spark_df[\"time\"], spark_df[\"value\"].min(), spark_df[\"value\"], color=status_colors[metric[\"status\"]], alpha=0.3\n    )\n    inset_ax.set_xticks([])\n    inset_ax.set_yticks([])\n    inset_ax.set_xlabel(\"\")\n    inset_ax.set_ylabel(\"\")\n    sns.despine(ax=inset_ax, left=True, bottom=True, top=True, right=True)\n\n    ax.set_xticks([])\n    ax.set_yticks([])\n    ax.set_xlim(0, 10)\n    ax.set_ylim(0, 10)\n    ax.grid(False)\n\n    # Metric name (top)\n    ax.text(5, 9.3, metric[\"name\"], fontsize=10, fontweight=\"bold\", color=INK_SOFT, ha=\"center\", va=\"top\")\n\n    # Main KPI value (center, prominent)\n    value_text = f\"{metric['value']:,}{metric['unit']}\" if metric[\"unit\"] else f\"{metric['value']:,}\"\n    ax.text(\n        5,\n        6.2,\n        value_text,\n        fontsize=26,\n        fontweight=\"bold\",\n        color=status_colors[metric[\"status\"]],\n        ha=\"center\",\n        va=\"center\",\n    )\n\n    # Change indicator with directional arrow\n    change = metric[\"change\"]\n    arrow = \"▲\" if change >= 0 else \"▼\"\n\n    # Lower is better for operational metrics; higher is better for usage/throughput\n    decrease_is_good = metric[\"name\"] in [\"CPU Usage\", \"Memory\", \"Response Time\", \"Error Rate\"]\n    if decrease_is_good:\n        change_color = \"#009E73\" if change < 0 else \"#BD8233\"\n    else:\n        change_color = \"#009E73\" if change >= 0 else \"#BD8233\"\n\n    ax.text(\n        5,\n        4.0,\n        f\"{arrow} {abs(change):.1f}%\",\n        fontsize=10,\n        fontweight=\"bold\",\n        color=change_color,\n        ha=\"center\",\n        va=\"center\",\n    )\n\n# Style\nfig.suptitle(\n    \"dashboard-metrics-tiles · python · seaborn · anyplot.ai\", fontsize=12, fontweight=\"medium\", color=INK, y=0.98\n)\n\nplt.tight_layout(rect=[0, 0, 1, 0.95])\n\n# Save — no bbox_inches so figsize×dpi stays exactly 3200×1800\nplt.savefig(f\"plot-{THEME}.png\", dpi=400, facecolor=PAGE_BG)\n"}