# Using pgfplots, how do I arrange my data matrix for a surface plot so that each cell in the matrix is plotted as a square?

I have a matrix of size MxN and I want to make a surface plot directly from above so that each cell in the matrix is drawn as a square. None of the pgfplots shaders really do what I want, since they draw squares between the cells of the matrix (i.e. a 3x3 matrix ends up as 2x2 cells when plotted). The example hopefully makes it clearer:

\documentclass{article}

\usepackage{fullpage}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

0 0 0
0 1 1
0 2 2

1 0 3
1 1 4
1 2 5

2 0 6
2 1 7
2 2 8
}\data

\begin{document}
\foreach \shader in {interp, flat, flat corner} {
\begin{tikzpicture}
\begin{axis}[
title style={
at={(0.5, 1.5)},
anchor=north,
},
width=.3\linewidth,
view={0}{90},
colormap/hot2,
colorbar horizontal,
colorbar style={
at={(0.5, 1.02)},
anchor=south,
xticklabel pos=upper,
},
]
\end{axis}
\end{tikzpicture}
}
\end{document}


How do I arrange my data so that each cell in the matrix is drawn as a separate cell in the output?

EDIT: I realised after entering the question that the answer is very simple, so I'll just answer this myself.

-

Looking at the flat corner plot in the question, it becomes obvious that one solution is to use flat corner and simply add an extra dummy row and an extra dummy column to the table. Their actual values will not influence the plot since flat corner uses only the value from the point which is inside the original matrix.

I am still interested in knowing other solutions, however, as the pgfplots manual specifies that "it is not defined which vertex is used here", making it a somewhat unstable solution to rely on the fact that it appears to use the point that I want.

-
This is an old feature request for pgfplots. I remember that it was not immediately clear how to switch from "vertex representation" to "cell representation" (i.e. how to interprete every data matrix as "cell representation") and I postponed the implementation. – Christian Feuersänger Sep 22 '12 at 18:06
I can live with the dummy row&column, but I'd really like to see the behaviour defined.... – DasKrümelmonster Mar 6 '14 at 22:17

Using the matrix plot* feature of PGFPlots v1.13 should exactly do what you want (see section 4.6.12 on page 164 of the manual).

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{
compat=1.13,
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis on top,
xmin=0,
xmax=2,
ymin=0,
ymax=2,
enlargelimits={abs=0.5},
point meta=explicit,
colormap/viridis,
colorbar,
]
matrix plot*,
] table [meta index=2] {
0 0 0
0 1 1
0 2 2

1 0 3
1 1 4
1 2 5

2 0 6
2 1 7
2 2 8
};
\end{axis}
\end{tikzpicture}
\end{document}


-