4

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?

3
  • 1
    Use pgfplots instead ?
    – percusse
    Commented 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
    Commented Jan 29, 2016 at 11:48
  • OK, I see the new answer by hbaderts.
    – Eric
    Commented Jan 29, 2016 at 11:50

3 Answers 3

11

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

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

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
]

    \addplot[black, ultra thick] {pow(x,2)-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:

result

15
  • Thanks for answering and giving the references! One more little question, how to make the downside of the parabola more smooth?
    – Eric
    Commented Jan 29, 2016 at 11:55
  • 1
    @Eric: Add something like [samples=200]or greater -not testet. Commented 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.
    – hbaderts
    Commented Jan 29, 2016 at 12:19
  • Instead of samples= you can simply add smooth to your axis options.
    – Alenanno
    Commented Jan 29, 2016 at 13:33
  • 1
    @Alenanno You should never smoothen closed form plots. They introduce junk.
    – percusse
    Commented Jan 29, 2016 at 14:23
2

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .