3

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?

  • I don't get warnings if I remove compat=1.10 and put compat=1.9 – user11232 Mar 8 '14 at 0:03
  • That is strange, I still get the warning (and the same behaviour) when running with compat=1.9 – vipers36 Mar 8 '14 at 13:09
  • Behavoiur is not good for me too, but no warning. – user11232 Mar 8 '14 at 13:11
  • @HarishKumar: You didn't get the warning because i had no 'nan' value in the code above. Sry, my bad. I've edited the code and the output – vipers36 Mar 19 '14 at 17:38
  • I'll add a todo item for pgfplots. That sounds like missing support in the surf/mesh plot handler. – Christian Feuersänger May 15 '14 at 17:07
4

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

  • 2
    Late comment: As PGF has a ceil function already, you can remove the declare function and use opacity=ceil(\pgfplotspointmetatransformed) (looks like it gives the same result at least). – Torbjørn T. May 15 '14 at 5:40
2

I don't have an answer, but I'd also like this, for a simple scatter plot:

(Example modified with the code suggested in the comment, it works!)

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot[scatter,
         only marks,
         point meta=explicit,
         point meta min=0,
         unbounded coords=discard,
         filter point/.code={
           \pgfmathparse{\pgfkeysvalueof{/data point/meta}}
           \let\A=\pgfmathresult
           \pgfmathfloatgetflagstomacro\A\flags
           \ifnum\flags=3
             \pgfkeyssetvalue{/data point/x}{nan}
           \fi
         },
] table[row sep=crcr,header=false,meta index=2] {
1 1 20 \\
2 2 nan \\
3 3 50 \\
4 4 100 \\
};
\end{axis}
\end{tikzpicture}
\end{document}

I don't want the second point drawn at all (but I still want to have the coordinates there, as I want it to be drawn in in other \addplot commands).

  • While looking for a solution for my problem, I found one for yours: Put this filter after unbounded coords=discard: ` filter point/.code={% \pgfmathparse {\pgfkeysvalueof{/data point/meta}} \let\A=\pgfmathresult \pgfmathfloatgetflagstomacro\A\flags \ifnum\flags=3 \pgfkeyssetvalue{/data point/x}{nan}% \fi },` It changes the x coordinate to nan if the meta data is nan. For more info on the filter: [pgfplots manual p.111 ](pgfplots.sourceforge.net/pgfplots.pdf). More info on the \pgfmathfloatgetflagstomacro stuff here: tex.stackexchange.com/a/114310/21530 – vipers36 Mar 20 '14 at 11:53
  • @vipers36 Thanks, it's included in my "reply" now. – Jellby Mar 20 '14 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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