[Tex/LaTex] pgfplots surf plot: don’t draw ‘NaN’

matlab2tikzpgfplots

I generated a checkerboard style suface plot using matlab2tikz.
The data contains nan (not a number) values. I want the squares of nan values to be white (as in matlab). When I compile, I get the following warning:

Package pgfplots Warning: The per point meta data `nan' (3Y0.0e0]) (and probabl
y others as well) is unbounded – using the minimum value instead. on input line
58.

and the square is filled with the colour for '0'. I tried to skip the nan values using

unbounded coords=discard

The warning and the behaviour stayed the same.
When removing the nan values, the square seems to take the color of its neighbour.

Here's the code generated by matlab2tikz:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}

\begin{tikzpicture}

\begin{axis}[%
view={0}{90},
scale only axis,
xmin=1, xmax=5,ymin=1,ymax=5,zmin=-1,zmax=1,
hide axis,
colormap/jet,
colorbar,
point meta min=1,
point meta max=100
]

\addplot3[%
surf,
shader=flat corner,
draw=black,
colormap/jet,
point meta=explicit,
%unbounded coords=discard,
mesh/rows=5]
table[row sep=crcr,header=false,meta index=3] {
    1   1   0   1  \\
    1   2   0   10  \\
    1   3   0   20  \\
    1   4   0   30  \\
    1   5   0   1  \\
    2   1   0   40  \\
    2   2   0   nan  \\
    2   3   0   50  \\
    2   4   0   60   \\
    2   5   0   1  \\
    3   1   0   70  \\
    3   2   0   80  \\
    3   3   0   90   \\
    3   4   0   nan  \\
    3   5   0   1  \\
    4   1   0   75  \\
    4   2   0   80  \\
    4   3   0   85  \\
    4   4   0   100  \\
    4   5   0   1  \\
    5   1   0   1  \\
    5   2   0   1  \\
    5   3   0   1   \\
    5   4   0   1   \\
    5   5   0   1   \\
};
\end{axis}
\end{tikzpicture}

\end{document}

And here the output:
enter image description here

Is there a way to not drawing the squares with nan values? Or to add a discrete color to the continuous colormap so that nan values are drawn in white?

Best Answer

I have found a quick n'dirty solution to the problem.

Instead of preventing nan fields from being drawn, I rendered them transparent. To do so, I set opacity of the fields depending on the point meta data. The opacity has to be = 0 for nan fields and >= 1 for other fields.

Therefore, the value of point meta min has to be slightly lower than the smallest point meta value (depending on the numerical resolution), so that ceil(pgfplotspointmetatransformed) >= 1 for the point with the smallest point meta.

The opacity is then set to opacity = ceil(\pgfplotspointmetatransformed). (ceil has to be defined) Since pgfplots sets all nan point meta values to point meta min, they appear transparent. Note that the border is not drawn for nan fields.

Here's the code:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}

\tikzset{
    declare function={Ceil(\x)=round(\x+0.49999);}
}

\begin{tikzpicture}

    \begin{axis}[%
        view={0}{90},
        scale only axis,
        xmin=1, xmax=5,ymin=1,ymax=5,zmin=-1,zmax=1,
        hide axis,
        colormap/jet,
        colorbar,
        point meta min=0.999999,
        point meta max=100
    ]

    \addplot3[%
        surf,
        shader=flat corner,
        draw=black,
        colormap/jet,
        point meta=explicit,
        opacity=Ceil(\pgfplotspointmetatransformed),
        mesh/rows=5
    ]
    table[row sep=crcr,header=false,meta index=3] {
        1   1   0   1  \\
        1   2   0   10  \\
        1   3   0   20  \\
        1   4   0   30  \\
        1   5   0   1  \\
        2   1   0   40  \\
        2   2   0   nan  \\
        2   3   0   50  \\
        2   4   0   60   \\
        2   5   0   1  \\
        3   1   0   70  \\
        3   2   0   80  \\
        3   3   0   90   \\
        3   4   0   nan  \\
        3   5   0   1  \\
        4   1   0   75  \\
        4   2   0   80  \\
        4   3   0   85  \\
        4   4   0   100  \\
        4   5   0   1  \\
        5   1   0   1  \\
        5   2   0   1  \\
        5   3   0   1   \\
        5   4   0   1   \\
        5   5   0   1   \\
};
\end{axis}
\end{tikzpicture}

\end{document}

And here the output: enter image description here

Related Question