# pgfplots 3D surface broke

I'm a newcomer to LaTeX and pgfplots. And now I found a problem while I was using \addplot3. It seems that the image behaved abnormally on perspective.This is the image generated.

For reference I changed to view={20}{0}, and it became the image below.

Could anyone tell me how to deal with it, please? I don't know how to make the first image right. My tex code is

%!TEX program = xelatex
\documentclass{article}
\usepackage{fontspec, xunicode, xltxtra, pgfplots}
\setmainfont{Hiragino Sans GB}
\pgfplotsset{compat=1.8}

\title{Title}
\author{}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
title={$x \exp(-x^2-y^2)$},
xlabel=$x$, ylabel=$y$,
small,
view={20}{30}
table {2.dat};
\end{axis}
\end{tikzpicture}
\caption{a small example}
\end{figure}

\end{document}


THANKS!

• Welcome to the site. I say print 1000 copies and sell it as an "M.C. Escher" original print. Good first question! – Steven B. Segletes May 13 '16 at 19:06
• Try putting z buffer=sort in your \addplot3 [...] options. – Jake May 13 '16 at 19:08
• Did you try z buffer= sort? – crateane May 13 '16 at 19:08
• Could you provide your data file? When plotting just the function {x*exp(-x*x-y*y)}, I could not reproduce the behavior... – crateane May 13 '16 at 20:24
• Hi, am author of pgfplots. Could you provide the data file 2.dat? pgfplots is supposed to draw plots like that correctly out of the box and I will see what I can do. If the file is too long (looks like 50x50), you can send it by mail (my address is in on top of the pgfplots manual). – Christian Feuersänger May 14 '16 at 6:51

As suggested in the comments, use the option z buffer = sort to correct the sorting in the data file. If you plot the function directly in pgfplots, the problem does not exist....

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
title={$x \exp(-x^2-y^2)$},
xlabel=$x$, ylabel=$y$,
small,
view={20}{20}
]