Visualizing contour lines in pgfplots

I want to visualize the set of zeros of $f(x,y)=x^2(x^2+y^2-2)$ in the same graphic that contains the plot of the function.

It is easy to see that this set is the union of the circle of radius sqrt(2) around (0,0) and the line x=0. I can plot both the set of zeros in TikZ and the plot of the function in pgfplots, however I don't know how to visualize the set of zeros within the plot of the function. Is there a way I can get the desired circle and the y-axis to stick out in some way? I thought of maybe plotting this set in red.

I don't know how to put my working example in the shaded box on stackexchange, so sorry if this looks like a mess:

\documentclass[12pt,ngerman]{article}
\usepackage[colorlinks,
pdfpagelabels,
pdfstartview = FitH,
bookmarksopen = true,
bookmarksnumbered = true,
linkcolor = black,
plainpages = false,
hypertexnames = false,
citecolor = black] {hyperref}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\usepackage{pgfplots}
\usetikzlibrary{patterns}

\makeatother

\begin{document}
\begin{figure}[H]
\centering{}\begin{tikzpicture}[scale=1.25]

\draw[step=.5cm, black!15, very thin] (-3,-3) grid (3,3);
\draw[pattern=north east lines, pattern color=blue!30, thick, draw=red] (0,0) circle (1.41);

\draw [thick] [->] (-2.5,0)--(2.5,0) node[right, below] {$x$};
\foreach \x in {-2,-1,1,2}
\draw[xshift=\x cm, thick] (0pt,-1pt)--(0pt,1pt) node[below] {$\x$};

\draw [thick] [->] (0,-2.5)--(0,2.5) node[above, left] {$y$};
\foreach \y in {-2,-1,1,2}
\draw[yshift=\y cm, thick] (-1pt,0pt)--(1pt,0pt) node[left] {$\y$};

\draw[thick, red] (0,-3)--(0,3);

\node[red] at (.5,2.5) {$=0$};
\node[blue] at (.5,.5) {$<0$};
\node at (2.5,2.5) {$>0$};

\end{tikzpicture}\begin{tikzpicture}
\begin{axis}[grid=both, view={10}{30}, xlabel=$x$, ylabel=$y$]
\addplot3[surf,shader=faceted] {x*x*(x*x+y*y-2};
\end{axis}
\end{tikzpicture}
\end{figure}

\end{document}


1 Answer

This is very easy. Provided you are using a version of 1.11 or newer, you can use add

\draw[thick,red] (0,0,0) circle[radius={sqrt(2)}] (0,-5,0) -- (0,5,0);


Alternatively, you could add plots with samples y=0,

\addplot3[color=red,samples y=0,domain=0:360] ({sqrt(2)*cos(x)},{sqrt(2)*sin(x)},0);
\addplot3[color=red,samples y=0,domain=-5:5] (0,x,0);


Full example:

\documentclass[12pt,ngerman]{article}
\usepackage[colorlinks,
pdfpagelabels,
pdfstartview = FitH,
bookmarksopen = true,
bookmarksnumbered = true,
linkcolor = black,
plainpages = false,
hypertexnames = false,
citecolor = black] {hyperref}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{patterns}

\makeatother

\begin{document}
\begin{figure}[htb]
\centering
\begin{tikzpicture}[scale=1.25]
\draw[step=.5cm, black!15, very thin] (-3,-3) grid (3,3);
\draw[pattern=north east lines, pattern color=blue!30, thick, draw=red] (0,0) circle (1.41);

\draw [thick] [->] (-2.5,0)--(2.5,0) node[right, below] {$x$};
\foreach \x in {-2,-1,1,2}
\draw[xshift=\x cm, thick] (0pt,-1pt)--(0pt,1pt) node[below] {$\x$};

\draw [thick] [->] (0,-2.5)--(0,2.5) node[above, left] {$y$};
\foreach \y in {-2,-1,1,2}
\draw[yshift=\y cm, thick] (-1pt,0pt)--(1pt,0pt) node[left] {$\y$};

\draw[thick, red] (0,-3)--(0,3);

\node[red] at (.5,2.5) {$=0$};
\node[blue] at (.5,.5) {$<0$};
\node at (2.5,2.5) {$>0$};

\end{tikzpicture}\begin{tikzpicture}
\begin{axis}[grid=both, view={10}{30}, xlabel=$x$, ylabel=$y$]
\addplot3[surf,shader=faceted] {x*x*(x*x+y*y-2};
\draw[thick,red] (0,0,0) circle[radius={sqrt(2)}] (0,-5,0) -- (0,5,0);
% \addplot3[color=red,samples y=0,domain=0:360] ({sqrt(2)*cos(x)},{sqrt(2)*sin(x)},0);
% \addplot3[color=red,samples y=0,domain=-5:5] (0,x,0);
\end{axis}
\end{tikzpicture}
\end{figure}

\end{document}


• Is this a parabolic soccer field? ;-) – Schrödinger's cat Oct 1 at 15:16
• lol, it would seem so, yes. But thanks for the fast answer! By 1.11 version you mean TikZ? Because the \draw command didnt work for me (the alternative version however did). – Buh Oct 1 at 15:26
• @Buh No, I mean \pgfplotsset{compat=1.16}. You didn't set any compatibility mode in your example, so it is running in backwards compatibility mode. Most likely you have at least \pgfplotsset{compat=1.15} on your machine anyway. It is very much recommended to run with the latest version since it fixes some issues which can become a pain. (You can draw on the axis with (axis cs:...) coordinates but the circle will be a bit more painful to obtain with that.) – Schrödinger's cat Oct 1 at 15:28
• I see. Thanks again. One last question: What does "samples y=0" mean? – Buh Oct 1 at 16:25
• @Buh It just means that what \addplot3 produces is one-dimensional, i.e. not a surface but a curve. – Schrödinger's cat Oct 1 at 20:09