# How to project a function over a 3D plane

When I looked up the method to express two variable functions as a sequence of intersecting horizontal planes, I thought to use PGFplots to construct a simple set of "slices" of a generic function (represented as a blue line).

Here is my MWE as the first plot (the first slice) is complied without errors:

\documentclass[a4paper]{article}
\usepackage{pgfplots}
%
\begin{document}
%
Initial \TeX{}t
\begin{figure}[h]
\begin{centering}
\begin{tikzpicture}
\tikzset{%
small dot/.style={fill=cyan,circle,scale=0.25}%
}
\begin{axis}[%
grid=major,
colormap/cool,
zlabel=$z$,
zmin=-5,
ztick={-5,-2.5,0,2.5,5},
ytick={-5,-2.5,0,2.5,5},
zmax=5,
xmin=-5,
xmax=5,
xlabel=$x$,
ylabel=$y$%
]
\node[small dot,pin=-60:{$z_1$}] at (axis cs:2.5,-5,0) {};
%
\end{axis}
\end{tikzpicture}
\par
\end{centering}
\caption{Funzione definita $f_{1}(x,y)$ o $z_{1}$}
\end{figure}
\par
Ending \TeX{}t
%
\end{document}


And the output is evidently correct: Now comes the major problem: I'd like to put ad additional layer with the specific function projected onto the new plane, but I have no idea on how to do this properly; as it can be seen the function on the MWE isn't really a function but some sort of "cheat".

The problem will be solved if a macro will be able to plot some function (i.e. sin(x)) onto a plane into the third dimension.
As I can see, this question is very similar to Tim N's Question (for comparison).

If the adding of a new layer will be too tricky, I'll accept the answer regarding the projection because it's the most important one, as the adding of more layers is easily solvable by re-correcting the position of the plots in my document.

So, your input is a 2d function f(x,y) and its surface, and you want to see its trace through z=0. This can be accomplished by means of a contour plot with level 0.

Here is a solution: \documentclass[a4paper]{article}
\usepackage{pgfplots}
%
\begin{document}
\thispagestyle{empty}
%
Initial \TeX{}t
\begin{figure}[h]
\begin{centering}
\begin{tikzpicture}
\tikzset{%
small dot/.style={fill=cyan,circle,scale=0.25}%
}
\begin{axis}[%
grid=major,
colormap/cool,
zlabel=$z$,
zmin=-5,
ztick={-5,-2.5,0,2.5,5},
ytick={-5,-2.5,0,2.5,5},
zmax=5,
xmin=-5,
xmax=5,
xlabel=$x$,
ylabel=$y$%
]

{exp(-x^2 -0.3*y^2 -0.2*x*y)*5- 0.1};

%
\end{axis}
\end{tikzpicture}
\par
\end{centering}
\caption{Funzione definita $f_{1}(x,y)$ o $z_{1}$}
\end{figure}
\par
Ending \TeX{}t
%
\end{document}


I took the freedom to discard the blue cross to show only the contour plot fragment. Note that I used some random surface which came to my mind, but any expression or table data is suitable here.

This example requires gnuplot as pgfplots is unable to compute the contour levels on its own. Consequently, you need to run it with -shell-escape (like pdflatex -shell-escape <file.tex>).

The argument labels=false deactivates contour text labels, the levels key chooses the requested contour levels (accepts a list), and draw color is necessary because otherwise, the current colormap would be used which is inadequate in this context.

EDIT the following is OPTIONAL.

For the records, here is my earlier (mis)understanding of the request. It addresses a slighty different use-case, namely one in which the 1d contour is given and the question is how to position it correctly.

From what I understand, you are given some 1d function f(x) which is given either as math expression (your example with sin(x)) or perhaps as data table, right? And you want that to be plotted into a 3D plane which happens to be orthogonal to one of the cube faces?

In this case, you can use either a parametric plot (if the function is to be sampled) or reorder the columns if the plot function is given as data table.

Here is your example with modifications: \documentclass[a4paper]{article}
\usepackage{pgfplots}
%
\begin{document}
\thispagestyle{empty}
%
Initial \TeX{}t
\begin{figure}[h]
\begin{centering}
\begin{tikzpicture}
\tikzset{%
small dot/.style={fill=cyan,circle,scale=0.25}%
}
\begin{axis}[%
grid=major,
colormap/cool,
zlabel=$z$,
zmin=-5,
ztick={-5,-2.5,0,2.5,5},
ytick={-5,-2.5,0,2.5,5},
zmax=5,
xmin=-5,
xmax=5,
xlabel=$x$,
ylabel=$y$%
]
{
X Y
-5 5
-4 -1
-3 0
-2 1
-1 1.2
0 1
1 2
2 3
4 1
5 0
};

\node[small dot,pin=-60:{$z_1$}] at (axis cs:2.5,-5,0) {};

(x,{4*sin(deg(x))},0);

%
\end{axis}
\end{tikzpicture}
\par
\end{centering}
\caption{Funzione definita $f_{1}(x,y)$ o $z_{1}$}
\end{figure}
\par
Ending \TeX{}t
%
\end{document}


The red plot is a parametric plot; you see that I provided x,y,and z coordinate values explicitly, z being zero here due to the z=0 plane. Note that samples y=1 is required because \addplot3 {<expression>} always samples a 2d function f(x,y). If we say samples y=1, we tell pgfplots that it actually has just one coordinate, i.e. that it should sample a line although it is a 3d plot.

The green plot is a table plot in which I mapped the table's two columns X and Y to the plane y=5 . Note that the ordering of plots is important.

Is this what you had in mind?

• @ Christian Feuersänger Well, I apologize if I didn't make a question enough understandable; but in the end I said that I have some 3D function that is intersected by a plane parallel to the xy plane, and the result slice (as blue) is projected onto the considered plane. For example, your red sinusoid works for my expectations. Aug 1, 2013 at 20:31
• OK. I must have missed the keyword "intersection" somewhere... perhaps you need to rely on contour plots: they are typically an "intersection" of a 3d surface with a plane. Aug 1, 2013 at 20:38
• @ Christian Feuersänger OK, I'll do some research on contour plots (thanks); but in your code the optional argument for addplot3 contains a parameter unknown to me: samples y=1. What is that supposed to mean? Aug 1, 2013 at 20:42
• I have edited my answer to accomodate (a) a contour plot solution and (b) explains samples y=1. Aug 1, 2013 at 21:06
• Now I know something more on PGFplots, maybe I'll post my usual anwer as a remark-full check on this bit of knowledge. Thanks again!. Aug 1, 2013 at 21:07