# Plotting level curves (contour curves)

So I managed to get this far with the knowledge I have of tikz and 3dplot and pgfplots :

\documentclass[11pt, oneside]{article}

\usepackage{tikz}
\usepackage{tikz-3dplot}
\usepackage{pgfplots}
\usepgfplotslibrary{colorbrewer,patchplots}
\pgfplotsset{width=8cm,compat=1.14}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
domain = -3:3,
y domain = -2:2,
view = {0}{90},
colormap={violet}{rgb=(0.3,0.06,0.5), rgb=(0.9,0.9,0.85)},
point meta max=5,
point meta min=-5,
]

contour filled={number = 100,labels={false}},
]{(\x)^2 - 4*(\y)^2};

\draw (0,-2) -- (0,2) node[left,yshift=-.2cm]{$y$};
\draw (-3,0) -- (3,0) node[below,xshift=-.2cm]{$x$};
\draw[color=gray!60!black,dashed] (-2,2) -- (2,-2);
\draw[color=gray!60!black,dashed] (-2,-2) -- (2,2);

\end{axis}
\end{tikzpicture}

\end{document}


Which yields out :

Now to make it perfect, I would only need to add the lines to show the contour of the graph, more specifically the likes of x^2 - 4*y^2 = 1 and x^2 - 4*y^2 = 2. Now this is where I am stuck and don't know how to do it. It should look something like this though :

Any help on this would be greatly appreciated and thanks in advance, also if I'm using the wrong tools to do what I want, you can always point me in the right direction.

P.S. I know the last picture looks odd, but I drew the lines in to show what it should resemble, though I would like it to represent the equations given above.

So this is what you are searching for? For details, please have a look at the comments in the code.

% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{
compat=1.15,
width=8cm,
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
view={0}{90},
domain=-3:3,
y domain=-2:2,
colormap={violet}{
rgb=(0.3,0.06,0.5),
rgb=(0.9,0.9,0.85)
},
point meta max=5,
point meta min=-5,
]

% changed how the surface is drawn
% this is the "conventional" way to do so
surf,
] {x^2 - 4 * y^2};

% add the contour lines
% increase a bit the number of samples so smooth' does a good job
samples=51,
samples y=51,
contour gnuplot={
% state at which levels you want to draw the contour lines
levels={1,2},
% we don't want to add labels
labels=false,
% they should be drawn in black
draw color=black,
% and they should be smoothed
handler/.style=smooth,
},
] {x^2 - 4 * y^2};

\draw (0,-2) -- (0,2) node [left,yshift=-.2cm]{$y$};
\draw (-3,0) -- (3,0) node [below,xshift=-.2cm]{$x$};
\draw [color=gray!60!black,dashed] (-2,2) -- (2,-2);
\draw [color=gray!60!black,dashed] (-2,-2) -- (2,2);

\end{axis}
\end{tikzpicture}
\end{document}


• Yes, this is exactly what I am looking for, though when I try to reproduce it gives me this error : Package pgfplots Error: sorry, plot file{test5_contourtmp0.table} could not be opened – Sephya Feb 16 '18 at 7:10
• You need a working gnuplot installation for my solution to work. I guess that you don't have gnuplot installed, right? – Stefan Pinnow Feb 16 '18 at 7:34
• ...and they also need to run with -shellescape` or whatever is called in their LaTeX environment (I think) – Rmano Feb 16 '18 at 7:52
• I do have gnuplot installed, though I don't know how to run the -shellescapte, I'll search on google for it – Sephya Feb 16 '18 at 8:03
• That will depend on the operating system / LaTeX distribution you have. Try to start form here: tex.stackexchange.com/questions/99475/… – Rmano Feb 16 '18 at 8:08