Heatmap
Machine Learning Confusion Matrix
Seamless confusion matrix for multi-class classification results
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.colors import LinearSegmentedColormap
fig, ax = plt.subplots(figsize=(10, 9), facecolor='#ffffff')
ax.set_facecolor('#ffffff')
np.random.seed(42)
classes = ['Cat', 'Dog', 'Bird', 'Fish', 'Rabbit', 'Hamster']
n = len(classes)
data = np.random.randint(0, 20, (n, n))
np.fill_diagonal(data, np.random.randint(70, 95, n))
# Modern gradient: Soft Lavender to Electric Violet
colors = ['#faf5ff', '#e9d5ff', '#c084fc', '#9333ea', '#581c87']
cmap = LinearSegmentedColormap.from_list('modern', colors, N=256)
im = ax.imshow(data, cmap=cmap, aspect='equal')
ax.set_xticks(range(n))
ax.set_yticks(range(n))
ax.set_xticklabels(classes, rotation=45, ha='right', color='#374151', fontsize=10)
ax.set_yticklabels(classes, color='#1f2937', fontsize=10, fontweight='500')
for i in range(n):
for j in range(n):
val = data[i, j]
color = '#ffffff' if val > 50 else '#1f2937'
ax.text(j, i, f'{val}', ha='center', va='center', color=color, fontsize=11, fontweight='bold')
cbar = plt.colorbar(im, ax=ax, shrink=0.8, pad=0.02)
cbar.set_label('Predictions', color='#1f2937', fontsize=11)
cbar.outline.set_edgecolor('#e5e7eb')
plt.setp(plt.getp(cbar.ax.axes, 'yticklabels'), color='#6b7280')
for spine in ax.spines.values():
spine.set_color('#e5e7eb')
ax.set_xlabel('Predicted Label', fontsize=12, color='#374151', fontweight='500')
ax.set_ylabel('True Label', fontsize=12, color='#374151', fontweight='500')
ax.set_title('Image Classification Confusion Matrix', fontsize=16, color='#111827', fontweight='bold', pad=15)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Heatmaps & Density
More Heatmap examples
☕