How to plot a confusion matrix with string axis rather than integer in Python?

A confusion matrix with string labels provides better readability than numeric indices. In Python, we can create such matrices using matplotlib and sklearn.metrics by customizing the axis labels with descriptive strings.

Basic Setup

First, let's import the required libraries and prepare sample data −

import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
import numpy as np

# Sample data - actual vs predicted classifications
actual = ['business', 'health', 'business', 'tech', 'health', 'tech']
predicted = ['business', 'business', 'business', 'tech', 'health', 'business']

# Define string labels
labels = ['business', 'health', 'tech']
print("Actual:", actual)
print("Predicted:", predicted)
print("Labels:", labels)
Actual: ['business', 'health', 'business', 'tech', 'health', 'tech']
Predicted: ['business', 'business', 'business', 'tech', 'health', 'business']
Labels: ['business', 'health', 'tech']

Creating Confusion Matrix with String Labels

Now we'll create the confusion matrix and plot it with string axis labels −

import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
import numpy as np

# Sample data
actual = ['business', 'health', 'business', 'tech', 'health', 'tech']
predicted = ['business', 'business', 'business', 'tech', 'health', 'business']
labels = ['business', 'health', 'tech']

# Create confusion matrix
cm = confusion_matrix(actual, predicted, labels=labels)

# Set up the plot
fig, ax = plt.subplots(figsize=(8, 6))
im = ax.imshow(cm, interpolation='nearest', cmap=plt.cm.Blues)

# Add colorbar
plt.colorbar(im)

# Set string labels for axes
ax.set_xticks(np.arange(len(labels)))
ax.set_yticks(np.arange(len(labels)))
ax.set_xticklabels(labels)
ax.set_yticklabels(labels)

# Add labels and title
ax.set_xlabel('Predicted Labels')
ax.set_ylabel('Actual Labels')
ax.set_title('Confusion Matrix with String Labels')

# Add text annotations
for i in range(len(labels)):
    for j in range(len(labels)):
        ax.text(j, i, str(cm[i, j]), ha='center', va='center', color='red')

plt.tight_layout()
plt.show()
Confusion Matrix displayed with:
- X-axis: business, health, tech (Predicted)
- Y-axis: business, health, tech (Actual)  
- Color-coded cells with numerical values

Enhanced Version with Seaborn

For a more professional appearance, we can use seaborn which provides better default styling −

import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
import seaborn as sns
import numpy as np

# Sample data
actual = ['business', 'health', 'business', 'tech', 'health', 'tech']
predicted = ['business', 'business', 'business', 'tech', 'health', 'business']
labels = ['business', 'health', 'tech']

# Create confusion matrix
cm = confusion_matrix(actual, predicted, labels=labels)

# Plot using seaborn heatmap
plt.figure(figsize=(8, 6))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues', 
            xticklabels=labels, yticklabels=labels)

plt.xlabel('Predicted Labels')
plt.ylabel('Actual Labels') 
plt.title('Confusion Matrix - Enhanced Version')
plt.tight_layout()
plt.show()
Enhanced confusion matrix with:
- Better color scheme and formatting
- Automatic value annotations
- Clean axis labels with string categories

Key Parameters

Parameter Purpose Example
labels Define string categories ['business', 'health', 'tech']
cmap Color scheme plt.cm.Blues, 'viridis'
annot=True Show values in cells Displays count in each cell

Conclusion

Creating confusion matrices with string labels improves interpretability by replacing numeric indices with meaningful category names. Use seaborn.heatmap() for enhanced styling or matplotlib.imshow() for basic plots.

Updated on: 2026-03-25T19:39:25+05:30

693 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements