# How to Plot Seaborn Confusion Matrix

In this article, we are going to learn how to draw a SeabHow to Plot Seaborn Confusion Matrix by 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.

### How to Plot Seaborn Confusion Matrix using 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:

```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. A confusion matrix can be plotted 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 heatmap() Confusion Matrix

We are going to see how we can use the heatmap() function to plot the Confusion Matrix using a given data.To plot Confusion Matrix we must have the data to plot. if this data is unformatted data, we must convert it into a panda dataframe where we align this data to a dataframe frame.

Once we have this formatted data in the form of a panda dataframe, we can plot this panda dataframe on the plot by using the heatmap() function. So let us see this with the help of a code example.

### Explanation:

import pandas as pd

• We are importing the seaborn module to our program.
• import seaborn as sns
• We are importing the panda’s module to our program.
• import pandas as pd
• We are importing the matplotlib module’s pyplot to our program to display the plot.
• import matplotlib.pyplot as pltsw
• We are taking a sample array of random values.
```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]]
```
• We are next converting this array to a panda dataframe with the size of 6 rows and 5 columns.
• DataFrame_Matrix = pd.DataFrame(array, range(6), range(5))
• Finally we are calling the heatmap() function on this dataframe , by just passing this dataframe to heatmap() function.
• sns.heatmap(DataFrame_Matrix, annot=True)
• The last and important step is to call the show function to show the plot on screen.
• pltsw.show()
```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)
pltsw.show()

```

Output

### 2. How to Plot Seaborn Confusion Matrix labels

In this second example, we are going to expand the heatmap() function with some more arguments. We will see how we can add more
information to our Seaborn heatmap() Confusion Matrix plot like labels, color, etc.

• The second argument can not is used to display the values on the plot. If you don’t pass this argument then values will not be visible on the plot.
• cmap is the third argument which means color map. This can have possible values like winter, summer, etc.
• cbar is the fourth argument which means color bar. This can be enabled or disabled by passing true or false.
• linewidths and line color are the arguments to set the width and color of the line. the square argument is used to set the shape of the cell, this can be set to true or false.
• xticklabels argument can be used to set the x-axis and yticklabels is used for y-axis labels for the plot. Let us see all these arguments with the below-working 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, cmap='summer', cbar=False, linewidths=3, linecolor='r', square=True, xticklabels=['a','b','c','d','e'],yticklabels=['x','y','z','p','q'])
pltsw.show()
```

Output