[Tex/LaTex] How to limit the range of a function in TikZ

tikz-pgf

I have code like this:

\documentclass[tikz]{standalone}

\begin{document}
\begin{tikzpicture}
    \draw [->,thick] (-5,0) -- (5,0) node[right] {$x$};
    \draw [->,thick] (0,-5) -- (0,5) node[above] {$y$};
    \draw[ultra thick, domain=-5:5] plot (\x, {pow(\x,2)-5});
\end{tikzpicture}
\end{document}

When I draw this, the parabola is extremely exceeding the area of what I want. Is there a way to limit the range of the function f(x)=x^2-5 to suit the coordinate area, i.e. only show the points which y-coordinates are between -5 and 5?

Best Answer

You could add a \clip before you do the plotting:

\documentclass[tikz, border=5mm]{standalone}

\begin{document}
  \begin{tikzpicture}
    \draw [->,thick] (-5,0) -- (5,0) node[right] {$x$};
    \draw [->,thick] (0,-5) -- (0,5) node[above] {$y$};
    \clip (-5,-5) rectangle (5,5);
    \draw[ultra thick, domain=-5:5] plot (\x, {pow(\x,2)-5});
  \end{tikzpicture}
\end{document}

rendered image