{"spec_id":"confusion-matrix","library":"seaborn","language":"python","code":"\"\"\" anyplot.ai\nconfusion-matrix: Confusion Matrix Heatmap\nLibrary: seaborn 0.13.2 | Python 3.13.13\nQuality: 82/100 | Updated: 2026-05-09\n\"\"\"\n\nimport os\n\nimport matplotlib.pyplot as plt\nimport numpy as np\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\"\n\n# Data - Multi-class classification results for a sentiment analysis model\nnp.random.seed(42)\nclass_names = [\"Negative\", \"Neutral\", \"Positive\", \"Mixed\"]\n\n# Create realistic confusion matrix with strong diagonal (good model)\n# but with some systematic confusion patterns\nconfusion_matrix = np.array(\n    [\n        [156, 12, 5, 8],  # Negative: mostly correct, some confused with Neutral\n        [18, 142, 15, 10],  # Neutral: hardest to classify, confused with all\n        [3, 8, 168, 6],  # Positive: good accuracy\n        [11, 14, 9, 125],  # Mixed: often confused with Neutral\n    ]\n)\n\n# Configure theme-adaptive styling\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# Create figure (square format for symmetric matrix)\nfig, ax = plt.subplots(figsize=(12, 12), facecolor=PAGE_BG)\nax.set_facecolor(PAGE_BG)\n\n# Create heatmap with annotations\nsns.heatmap(\n    confusion_matrix,\n    annot=True,\n    fmt=\"d\",\n    cmap=\"Blues\",\n    xticklabels=class_names,\n    yticklabels=class_names,\n    square=True,\n    linewidths=2,\n    linecolor=PAGE_BG,\n    cbar_kws={\"shrink\": 0.8},\n    annot_kws={\"size\": 20, \"weight\": \"bold\"},\n    ax=ax,\n)\n\n# Style the colorbar\ncbar = ax.collections[0].colorbar\ncbar.ax.tick_params(labelsize=16, colors=INK_SOFT)\ncbar.ax.set_ylabel(\"Count\", fontsize=18, labelpad=15, color=INK)\n\n# Labels and title\nax.set_xlabel(\"Predicted Label\", fontsize=22, labelpad=15, color=INK)\nax.set_ylabel(\"True Label\", fontsize=22, labelpad=15, color=INK)\nax.set_title(\"Sentiment Analysis · confusion-matrix · seaborn · anyplot.ai\", fontsize=24, pad=20, color=INK)\n\n# Style tick labels\nax.tick_params(axis=\"both\", labelsize=18, colors=INK_SOFT)\n\n# Rotate x-axis labels for better readability\nplt.xticks(rotation=45, ha=\"right\")\nplt.yticks(rotation=0)\n\nplt.tight_layout()\nplt.savefig(f\"plot-{THEME}.png\", dpi=300, bbox_inches=\"tight\", facecolor=PAGE_BG)\n"}