# How to limit the range of a function in TikZ?

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?

• Use pgfplots instead ? Jan 29, 2016 at 11:42
• Thanks. I'm pretty new to TikZ and pgf. I don't know in what circumstance had I better use pgfplots. Is TikZ not enough to use in some cases?
– Eric
Jan 29, 2016 at 11:48
– Eric
Jan 29, 2016 at 11:50

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}


• Thanks for answering! I think I should try to cope with pgfplots.
– Eric
Jan 29, 2016 at 11:59

Though the internal plot functions of TikZ work well for simple plots, I'd recommend using the PGFPlots package for more advanced plots. You can find the manual here, and a nice gallery with examples of everything you can think of here.

With PGFPlots, your plot looks like this:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=6cm,
height=6cm,
title={A test plot},
xlabel={x},
ylabel={y},
axis lines=middle,
domain=-5:5,
samples=100,
xmin=-5, xmax=5,
ymin=-5, ymax=5
]

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


By default, PGFPlots draws a rectangle around the axis (see the examples page). To have single lines for the axis, use axis lines=middle.

With \pgfplotsset{compat=1.13}, you tell PGFPlots to use version 1.13. I'd suggest you to set this value to the newest value you can. As long as you use a version >= 1.11, you can use normal TikZ commands, like \draw (0,0) -- (1,1);, and the same coordinates as in the plot are used.

The result:

• Thanks for answering and giving the references! One more little question, how to make the downside of the parabola more smooth?
– Eric
Jan 29, 2016 at 11:55
• @Eric: Add something like [samples=200]or greater -not testet. Jan 29, 2016 at 12:12
• Exactly, as Hans-Peter E. Kristiansen says, with samples=... you can define the number of points. I added this to the answer. Note: with more points, the compilation time increases! I usually use 100, as this is often enough for normally-sized plots. Jan 29, 2016 at 12:19
• Instead of samples= you can simply add smooth to your axis options. Jan 29, 2016 at 13:33
• @Alenanno You should never smoothen closed form plots. They introduce junk. Jan 29, 2016 at 14:23

If your LaTeX installation is able to call Gnuplot as an external program you could replace

\draw[ultra thick, domain=-5:5] plot (\x, {pow(\x,2)-5});


by

\draw[ultra thick] plot[domain=-5:5, yrange=-5:5] function{x*x-5};


See the section "Plotting a Function Using Gnuplot" in the TikZ/PGF manual.