Take the 2-minute tour ×
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.

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}

\pgfplotstableread{
        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={\texttt{\shader}},
        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,
        },
]
\addplot3 [surf, shader=\shader] table {\data};
\end{axis}
\end{tikzpicture}
}
\end{document}

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
add comment

1 Answer 1

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
7  
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 at 22:17
add comment

Your Answer

 
discard

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.