# How to draw hyperbola with addplot

I want to draw hyperbola with pgfplots:

(x+1)^2/4-(y-2)^2/9=1

• Have you tried anything? This site usually works best when you present your best attempt. It's also polite to give us a minimal working example that we can start from. – Teepeemm Aug 13 '16 at 2:29
• @Teepeemm I'm starting to use TikZ , so I do not show any initial work. – casio Aug 13 '16 at 2:31
• The easiest solution is to use (y-2)^2/9=(x+1)^2/4-1 and take the square root (plus or minus). Note that x<-3 or x>1, so you will need four \addplots total. – John Kormylo Aug 13 '16 at 3:35

As stated in comments, you can reformulate the equation as "y = f(x)" with the problem of roots.

An alternative is to interprete it as "f(x,y)= (x+1)^2/4-(y-2)^2/9" and draw the contour "f(x,y) = 1" :

\documentclass{standalone}

\usepackage{pgfplots}

\pgfplotsset{compat=1.13}

\begin{document}

\begin{tikzpicture}
\begin{axis}[view={0}{90}]
contour gnuplot={labels=false,levels={1}}
] {(x+1)^2/4-(y-2)^2/9};
\end{axis}
\end{tikzpicture}
\end{document}


The value domain=-10:10 determines the computed range. Since domain y is missing, pgfplots assumes that it should also use -10:10.

Note that you need to compile this by means of pdflatex -shell-escape file.tex and you need gnuplot installed.

Note that the approach as such works for any kind of plot program, not just pgfplots.

I would strongly recommend googling around for a tikz/pgf tutorial to learn at least the basics on your own. You'll learn much more that way.

The most straightforward approach here is to solve for y=2±√(9*(x+1)^2/4-9) with x < -3 or 1 < x. Plotting this requires four commands, and results in

\begin{tikzpicture}
\begin{axis}[xmin=-10,xmax=10,ymin=-10,ymax=10]
\end{axis}
\end{tikzpicture}


This is not a great plot (there's the obvious gap on the left, and if you look closer, you'll see that the right has a few straight segments and angles near the vertex). To fix this, we can change to a parametric plot: x=2*sec(t)-1, y=3*tan(t)+2. Plotting this still requires two commands (to avoid the vertical asymptotes of sec and tan) and results in:

\documentclass{standalone}

\usepackage{pgfplots}

\pgfplotsset{compat=1.13}

\begin{document}

\begin{tikzpicture}
\begin{axis}[xmin=-10,xmax=10,ymin=-10,ymax=10]
\end{axis}
\end{tikzpicture}

\end{document}


\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin=-10,xmax=10, ymin=-15, ymax=15,
restrict x to domain=-20:20]% remove crossing lines at t=90 and t=270