9

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}
\pgfplotsset{compat=newest}
%
\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$%
]
\addplot3 [surf] {0};
\addplot3 [color=blue] coordinates {(0,-5,0) (0,5,0)};
\addplot3 [color=blue] coordinates {(-5,0,0) (5,0,0)};
\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:

enter image description here

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.

1 Answer 1

10

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:

enter image description here

\documentclass[a4paper]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
%
\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$%
]

\addplot3 [surf] {0};

\addplot3[contour gnuplot={labels=false,levels={0},draw color=blue},domain=-4:4]
    {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:

enter image description here

\documentclass[a4paper]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
%
\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$%
]
\addplot3[green] table[x=X,y expr=5,z=Y]
{
X Y
-5 5
-4 -1
-3 0
-2 1
-1 1.2
0 1
1 2
2 3
4 1
5 0
};

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


\addplot3[red,samples y=1]
    (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?

5
  • @ 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.
    – TheVal
    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?
    – TheVal
    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!.
    – TheVal
    Aug 1, 2013 at 21:07

You must log in to answer this question.

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