An example of how to create and plot a confusion matrix (or crosstab) from dataframe columns using pandas in python:
Table of contents
Create a datafrrame
import pandas as pddata = {'prediction':['a','a','a','b','b','b','c','c','c'],'actual':['a','a','b','b','b','b','b','c','c']}df = pd.DataFrame(data)print(df)prediction actual0 a a1 a a2 a b3 b b4 b b5 b b6 c b7 c c8 c c
Create a confusion table
contingency_matrix = pd.crosstab(df['prediction'], df['actual'])print(contingency_matrix)actual a b cpredictiona 2 1 0b 0 3 0c 0 1 2
Plot the confusion table
import matplotlib.pyplot as pltimport seaborn as snplt.clf()ax = fig.add_subplot(111)ax.set_aspect(1)res = sn.heatmap(contingency_matrix.T, annot=True, fmt='.2f', cmap="YlGnBu", cbar=False)plt.savefig("crosstab_pandas.png", bbox_inches='tight', dpi=100)plt.show()

