In this article, we are going to learn how to Plot Seaborn Confusion Matrix with Custom labels using the Seaborn module’s heatmap() function. We will learn this with the help of coding examples and the visual output in the form of a plot by using some sample data.
Confusion Matrix plot using Seaborn’s heatmap() function
Here we are going to see all our examples using Seaborn’s heatmap() function. Let us learn about this function first with its syntax as:
Syntax
seaborn.heatmap(data, *, vmin=None, vmax=None, cmap=None, center=None, robust=False, annot=None,
fmt='.2g', annot_kws=None, linewidths=0, linecolor='white', cbar=True, cbar_kws=None, cbar_ax=None,
square=False, xticklabels='auto', yticklabels='auto', mask=None, ax=None, **kwargs)¶
You can learn about all its arguments at
What is a confusion matrix?
A confusion matrix is a table that is often used to describe the performance of a classification model.Confusion matrix can be plot on a set of data for which the true values are known or can be predicted.This helps you understand the possible predictions by just looking at the plot.
- Seaborn Single Line Plot Pandas Dataframe
- Seaborn Multiple Line Plot Pandas DataFrame
- How to Plot Seaborn Confusion Matrix
Plot Seaborn Confusion Matrix with % percentage
We are going to see how we can use the heatmap() function to plot the Confusion Matrix using a given data. We are going to display the data in each square with the percentage representation.To do this, we will need to use the fmt argument of the heatmap() function. We will specify the .2% format specified to present the data in percentage on Confusion Matrix. so let us see this with the help of a code example.
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as pltsw
array = [[1,10,0,2,0],
[3,5,0,1,0],
[0,11,3,0,2],
[0,0,12,0,0],
[0,9,0,13,0],
[0,13,0,0,11]]
DataFrame_Matrix = pd.DataFrame(array, range(6), range(5))
sns.heatmap(DataFrame_Matrix, annot=True,fmt='.2%', cmap='Blues')
pltsw.show()
Output

How to plot Seaborn Confusion Matrix with Custom labels
In this second example, we are going to use the heatmap() function to plot a Confusion Matrix with labels for each square box. Let us understand this with the help of the below code example.
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as pltsw
array = [[5, 50],
[ 3, 30]]
DataFrame_Matrix = pd.DataFrame(array, range(2), range(2))
labels = ['True','False','False','True']
labels = np.asarray(labels).reshape(2,2)
sns.heatmap(DataFrame_Matrix, annot=labels,fmt='', cmap='summer', cbar=False, linewidths=3, linecolor='r', square=True)
pltsw.show()
Output:

Seaborn Confusion Matrix with Custom labels
In this example, we are going to learn how to add multiple labels to Confusion Matrix squares. In the above two examples, we saw two types of labels, one is in the form of text 1 and another is just text 2 label. Now, if we want to add both these labels to the same Confusion Matrix. then how this can be done. We will need to create custom labels for the matrix as given in the below code example:
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as pltsw
array = [[5, 50],
[ 3, 30]]
DataFrame_Matrix = pd.DataFrame(array, range(2), range(2))
Text_label = ['True','False','False','True']
Text1_label = ['Positive','Negative','Negative','Positive']
labels = [f"{L1}\n{L2}" for L1, L2 in
zip(Text_label,Text1_label)]
labels = np.asarray(labels).reshape(2,2)
sns.heatmap(DataFrame_Matrix, annot=labels,fmt='', cmap='summer', cbar=False, linewidths=3, linecolor='r', square=True)
pltsw.show()
Output:
