2

So I would like to present data using a color plot subdivided into squares. For a large data set, it is easy to use a 3D plot with surf, however when there is not so much information, this does not work so nicely.

Take for example:

\begin{tikzpicture}
\begin{axis}[view={0}{90}]
    \addplot3 [surf]
        coordinates {
            (0, 0, 1) (1, 0, 9) (2, 0, 3)

            (0, 1, 4) (1, 1, 5) (2, 1, 6)

            (0, 2, 7) (1, 2, 2) (2, 2, 9)};
\end{axis}
\end{tikzpicture}

Naive approach

I have 9 data points, and 4 squares are displayed which reduces greatly the amount of data displayed.

An alternative I thought of was to use square marks

\begin{tikzpicture}
\begin{axis}[view={0}{90}, enlargelimits=.3]
    \addplot3 [scatter, mark=square*, mark size=24pt]
        coordinates {
            (0, 0, 1) (1, 0, 9) (2, 0, 3)

            (0, 1, 4) (1, 1, 5) (2, 1, 6)

            (0, 2, 7) (1, 2, 2) (2, 2, 9)};
\end{axis}
\end{tikzpicture}

Using marks

but the result is less than pleasing, and would require a lot of mucking around with the size of the marks.

The only "alternative" is to use interpolation so that at the vertices the correct color is displayed

\begin{tikzpicture}
\begin{axis}[view={0}{90}]
    \addplot3 [surf, shader=interp]
        coordinates {
            (0, 0, 1) (1, 0, 9) (2, 0, 3)

            (0, 1, 4) (1, 1, 5) (2, 1, 6)

            (0, 2, 7) (1, 2, 2) (2, 2, 9)};
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[view={0}{90}]
    \addplot3 [surf, shader=interp, patch type=bilinear]
        coordinates {
            (0, 0, 1) (1, 0, 9) (2, 0, 3)

            (0, 1, 4) (1, 1, 5) (2, 1, 6)

            (0, 2, 7) (1, 2, 2) (2, 2, 9)};
\end{axis}
\end{tikzpicture}

Interpolation

But without the correct interpolation, looks ugly. Besides, it doesn't exactly fix the problem of displaying only 9 data points.

So for small data sets, is there a way of plotting exactly the same number of squares as required?

  • Well, I tried to search tex.sx for a solution to this issue, though I did not see that one. Indeed, it is a duplicate, and using flat corner is a solution, though not exactly ideal as it gives the impression that the data points are actually between the vertices. It is still significantly better though. I gave it more thought, and is it possible to define your own marker so that the width is exactly half of the distance separating vertices, and have the vertical/horizontal scalings independent? That would fix the issue. Also, should I post this suggestion in the duplicate? – JP-Ellis Aug 12 '13 at 18:52
  • Well, you can say mark size=1 (without unit), provided that \pgfplotsset{compat=<version>} is high enough (1.8 will do). In that case, pgfplots will take one axis unit. I believe there was some solution (probably of @Jake) which relied on this feature. And: yes, I suppose flagging this as dupe is a good idea - you can post work-arounds or answers on the original one. – Christian Feuersänger Aug 12 '13 at 19:01
  • Just one last thing regarding your last reply, but using mark size=1 does not seem to work. I am using \pgfplotsset{compat=1.8}, so I don't know what is the issue. Also, the documentation does not seem to show mark size=1 as scaling to one unit. Also, this would not allow x and y scaling to be specified individually? Unless perhaps the cube marker is used? – JP-Ellis Aug 12 '13 at 19:29
  • 2
    @Josh: You might want to take a look at matlab2tikz imagesc TikZ/pgfplots equivalent (I believe that's the question Christian had in mind). – Jake Aug 13 '13 at 8:48
  • @Josh my bad: sorry, the mark size=1 hint was wrong; I must have mistaken it for bar width (which accepts axis units). Please excuse me. And yes, I meant the link posted by Jake, perhaps it helps you. – Christian Feuersänger Aug 13 '13 at 18:28

Browse other questions tagged or ask your own question.