4

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

6
  • 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
    Commented Nov 20, 2014 at 13:09
  • Like this? i.sstatic.net/6gETo.png. by clipping.
    – user11232
    Commented Nov 20, 2014 at 13:51
  • Yes, I think this is what I'm looking for. Could you provide an easy example of how to use clipping?
    – Fabian
    Commented Nov 20, 2014 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
    Commented Nov 20, 2014 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.
    – Ignasi
    Commented Nov 22, 2014 at 7:35

1 Answer 1

3

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};

enter image description here

\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}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .