1

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}
\usetikzlibrary{pgfplots.groupplots,calc,shadings,patterns,tikzmark, plotmarks, spy, pgfplots.polar, matrix, shapes.symbols,shadings,shapes, decorations.shapes,decorations.pathmorphing,fit,backgrounds}
%\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[]
\addplot3[surf,domain=0:1, samples=20] { x};

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

\end{groupplot}
\end{tikzpicture}

\end{document}

Which results in

enter image description here

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

2

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
ed because it is unbounded (in x). (see also unbounded coords=jump).

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}
\usetikzlibrary{pgfplots.groupplots,calc,shadings,patterns,tikzmark, plotmarks, spy, pgfplots.polar, matrix, shapes.symbols,shadings,shapes, decorations.shapes,decorations.pathmorphing,fit,backgrounds}
%\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[]
\addplot3[surf,domain=0:1, samples=20] { x};

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

\end{groupplot}
\end{tikzpicture}

\end{document}

And the result is:

enter image description here

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.

3
  • 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?
    – aaragon
    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
  • 1
    There's this post that describes how to parametrically map the (x,y) coordinates to a triangle.
    – aaragon
    Dec 29 '17 at 14:15

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.