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?

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.


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,
        ]
            \addplot [
                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}

image showing the result of above code

Tags:

Pgfplots