# pgfplot addplot3 over a triangular region

I would like to restrict a surface plot to a portion of the x-y plane (the triangular region x + y < 1). It seems that the filter point/.code is almost doing its job, if it weren't for the horrible result I get. This is the code I have so far:

\documentclass[border={10pt 10pt 10pt 10pt}]{standalone}

\usepackage{graphicx}
\usepackage[dvipsnames,svgnames,table]{xcolor} % use color
\usepackage{amsmath,amsfonts,amssymb,amsthm} % For math equations, theorems, symbols, etc

\usepackage{tikz}
\usepackage[customcolors]{hf-tikz}
\usepackage[nodisplayskipstretch]{setspace}
\usepackage{pgfplots}
%\tikzexternalize[prefix=./figures/tikz/]
\usepgflibrary{shapes.geometric}
\usepgfplotslibrary{colorbrewer}

\begin{document}

\begin{tikzpicture}

\begin{groupplot}[
group style={
group name=my plots,
group size=3 by 1,
xlabels at=edge bottom,
xticklabels at=edge bottom,
vertical sep=5pt
},
colormap/viridis,
axis lines*=left,
width=8cm,
xmin=0,xmax=1,
ymin=0,ymax=1,
zmin=0,zmax=1,
view={45}{45},
axis line style={draw=none},
tick style={draw=none},
ticks=none,
filter point/.code={%
\pgfmathparse
{\pgfkeysvalueof{/data point/x} + \pgfkeysvalueof{/data point/y} > 1.0}%
\ifpgfmathfloatcomparison
\pgfkeyssetvalue{/data point/x}{nan}%
\fi
},
]

% enrichments
\nextgroupplot[]
\addplot3[surf,domain=0:1, samples=20] { 1 - x - y};

\nextgroupplot[]

\nextgroupplot[]

\end{groupplot}
\end{tikzpicture}

\end{document}


Which results in

What am I doing wrong? Is there a way to get a smooth plot?

If we read the .log we'll see that there is a message:

NOTE: coordinate (1Y7.8945007e-1],1Y9.999701e-1],1Y9.999701e-1]) has been dropp


So I just added an option unbounded coords=jump and it seems to work perfectly:

\documentclass[border={10pt 10pt 10pt 10pt}]{standalone}

\usepackage{graphicx}
\usepackage[dvipsnames,svgnames,table]{xcolor} % use color
\usepackage{amsmath,amsfonts,amssymb,amsthm} % For math equations, theorems, symbols, etc

\usepackage{tikz}
\usepackage[customcolors]{hf-tikz}
\usepackage[nodisplayskipstretch]{setspace}
\usepackage{pgfplots}
%\tikzexternalize[prefix=./figures/tikz/]
\usepgflibrary{shapes.geometric}
\usepgfplotslibrary{colorbrewer}

\begin{document}

\begin{tikzpicture}

\begin{groupplot}[
group style={
group name=my plots,
group size=3 by 1,
xlabels at=edge bottom,
xticklabels at=edge bottom,
vertical sep=5pt
},
colormap/viridis,
axis lines*=left,
width=8cm,
xmin=0,xmax=1,
ymin=0,ymax=1,
zmin=0,zmax=1,
view={45}{45},
axis line style={draw=none},
tick style={draw=none},
ticks=none,
unbounded coords=jump,% <<<<< Inserted this here!
filter point/.code={%
\pgfmathparse
{\pgfkeysvalueof{/data point/x} + \pgfkeysvalueof{/data point/y} > 1.0}%
\ifpgfmathfloatcomparison
\pgfkeyssetvalue{/data point/x}{nan}%
\fi
},
]

% enrichments
\nextgroupplot[]
\addplot3[surf,domain=0:1, samples=20] { 1 - x - y};

\nextgroupplot[]

\nextgroupplot[]

\end{groupplot}
\end{tikzpicture}

\end{document}


And the result is:

The reason, as I discovered in this answer, is that when you and nans, PGF's default behavior is to try and connect the graph points (unbounded coords=skip). That's why, in this case, we need to change the behavior to jump.

• Thanks for your answer. That indeed improves the situation, but the figure is still not good because of the stair casing along the diagonal. Do you know how to get rid of that? Dec 28 '17 at 13:52
• I think that in this case you would need to reformulate the way you are building the surface. As I understood it, you draw the surface and apply a mask to it (correct me if I'm wrong). Since the surface is made of discrete points, the masking will give you this staircase effect. The easiest (and most stupid) way I cant think of is to refine your surface by increasing the number of samples in your \addplot3 commands... Dec 28 '17 at 14:03
• There's this post that describes how to parametrically map the (x,y) coordinates to a triangle. Dec 29 '17 at 14:15