TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:



        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

\foreach \shader in {interp, flat, flat corner} {
        title style={
                at={(0.5, 1.5)},
        colorbar horizontal,
        colorbar style={
                at={(0.5, 1.02)},
                xticklabel pos=upper,
\addplot3 [surf, shader=\shader] table {\data};

enter image description here

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.

share|improve this question
up vote 6 down vote accepted

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.

share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.