# Create confusion matrix in Latex

I want to Create confusion matrix 5x5 in Latex (overleaf), background same color blue.

I used this code:

\begin{tikzpicture}
\begin{axis}[
colormap={bluewhite}{color=(white) rgb255=(90,96,191)},
xlabel=Predicted,
xlabel style={yshift=-30pt},
ylabel=Actual,
ylabel style={yshift=20pt},
xticklabels={W1, W2, W3, W4},
xtick={0,...,3},
xtick style={draw=none},
yticklabels={W1, W2, W3, W4},
ytick={0,...,3},
ytick style={draw=none},
enlargelimits=false,
colorbar,
xticklabel style={
rotate=90
},
nodes near coords={\pgfmathprintnumber\pgfplotspointmeta},
nodes near coords style={
yshift=-7pt
},
]
matrix plot,
mesh/cols=4,
point meta=explicit,draw=gray
] table [meta=C] {
x y C
0 0 0.96
1 0 0.1
2 0 0
3 0 0

0 1 0.1
1 1 0.98
2 1 1
3 1 0.1

0 2 0
1 2 0
2 2 0.97
3 2 0.3

0 3 0.3
1 3 0
2 3 0
3 3 1

};
\end{axis}
\end{tikzpicture}


But got this output:

How I can add fifth column ( W5)?

• What have you tried? Could you edit into your question any code you have made trying to make this matrix please.
– user245306
Apr 10, 2022 at 20:55
• Does this answer your question? How to construct a confusion matrix in LaTeX? Apr 10, 2022 at 21:43
– user258528
Apr 11, 2022 at 12:38
• It still feels like you've not tried. Even without understanding the code, I can get the fifth column without much effort. What is your understanding of the \addplot[]table{} macro? Apr 11, 2022 at 13:18
• I can't get it ? please any help
– user258528
Apr 11, 2022 at 14:21

I'm still confused about why you were confused. I essentially looked through the code for every "4" (or "3") and incremented. The only difficulty is that I couldn't have comments within the \addplot.

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
colormap={bluewhite}{color=(white) rgb255=(90,96,191)},
xlabel=Predicted,
xlabel style={yshift=-30pt},
ylabel=Actual,
ylabel style={yshift=20pt},
xticklabels={W1, W2, W3, W4, W5}, % changed
xtick={0,...,4}, % changed
xtick style={draw=none},
yticklabels={W1, W2, W3, W4, W5}, % changed
ytick={0,...,4}, % changed
ytick style={draw=none},
enlargelimits=false,
colorbar,
xticklabel style={
rotate=90
},
nodes near coords={\pgfmathprintnumber\pgfplotspointmeta},
nodes near coords style={
yshift=-7pt
},
]
matrix plot,
mesh/cols=5, % changed
point meta=explicit,draw=gray
] table [meta=C] {
x y C
0 0 0.96
1 0 0.1
2 0 0
3 0 0
4 0 0

0 1 0.1
1 1 0.98
2 1 1
3 1 0.1
4 1 0.02

0 2 0
1 2 0
2 2 0.97
3 2 0.3
4 2 0

0 3 0.3
1 3 0
2 3 0
3 3 1
4 3 0.01

0 4 0
1 4 0
2 4 0.01
3 4 0
4 4 0.95

}; % added every entry where x=4 or y=4
\end{axis}
\end{tikzpicture}
\end{document}


For an output of