# Plotting a function on a triangular domain using pgfplots

I would like to draw a function on a triangular domain using tikz. For example, I would like to draw the function x*y on (-1, -1) -- (-1, 1) -- (1, -1).

This is a MWE for the rectangular domain (-1, 1) x (-1, 1).

\documentclass[10pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xlabel=$x$,ylabel=$y$,small,view={120}{40}]
\addplot3[surf,domain=-1:1,domain y=-1:1] {x*y};
\end{axis}
\end{tikzpicture}
\end{document}


How can I draw this function only on a triangular part of the domain?

Cheers

Thank you for your replies.

One follow-up question: I noticed that when clipping several functions using addplot3, depending on the view, the plots often disappear. Consider e.g.

\documentclass[10pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xlabel=$x$,ylabel=$y$,small,view={-45}{10},zmin=-1,zmax=1]
\clip (axis cs:-1,-1) -- (axis cs:1,1) -- (axis cs:-1,1) -- cycle;
\addplot3[surf,domain=-1:1,domain y=-1:1,color=gray,faceted color=black,opacity=0.05] {x*y};
\addplot3[surf,domain=-1:1,domain y=-1:1,color=red,faceted color=red,opacity=0.05] {(2*x*x+2*x+2*y*y-2*y)/(2*x-2*y+4)};
\addplot3[patch,patch type=triangle,color=blue,faceted color=blue,fill opacity=0.2] coordinates {(-1,-1,1) (1,1,1) (-1,1,-1)};
\end{axis}
\end{tikzpicture}
\end{document}


If I change

view={-45}{10}


to

view={0}{90}


you can see the plots. However, with the aforementioned view, you cannot. Why is that?

Cheers

• Welcome to TeX.SX!. When asking questions it is better to provide a full minimal working example (MWE) both in order to demonstrate what you are trying to do and to help others help you. The MWE should look like \documentclass...\begin{document}...\end{document}, it should compile and contain close to the minimal amount of code needed to explain/demonstrate what you are asking. This saves everyone time:)
– user30471
Nov 20 '14 at 13:09
• Like this? i.stack.imgur.com/6gETo.png. by clipping.
– user11232
Nov 20 '14 at 13:51
• Yes, I think this is what I'm looking for. Could you provide an easy example of how to use clipping? Nov 20 '14 at 22:24
• It is easy. See the answer below. BTW if you add @<username> (as in @HarishKumar) that user will be notified about the comment.
– user11232
Nov 20 '14 at 23:49
• Fabian, it's better that you start new questions, making mention of previous ones if necessary, for follow up questions. They will receive more atention. Nov 22 '14 at 7:35

## 1 Answer

One way would be to plot the curve and clip it by

\clip  (-1, -1) -- (-1, 1) -- (1, -1) -- cycle;
\addplot3[surf,domain=-1:1,y domain=-1:1] {x*y};


\documentclass[10pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xlabel=$x$,ylabel=$y$,small,view={120}{40}]
\clip  (-1, -1) -- (-1, 1) -- (1, -1) -- cycle;
\addplot3[surf,domain=-1:1,y domain=-1:1] {x*y};
\end{axis}
\end{tikzpicture}
\end{document}