My function does not plot the proper range

I am trying to plot a simple function: y = sqrt(x+4)-2:

\begin{tikzpicture}
\begin{axis}[
xlabel=$x$,
ylabel={$y$},
axis x line=center, axis y line=center
]
color=red] {sqrt(x+4)-2};
\end{axis}
\end{tikzpicture}


I get:

The min y value should be -2 at x = -4, but I get -1.5. How is that? Thank you!

• Well, you ask pgfplots to plot this all the way down to -5 by saying domain=-5:5. What should it plot below x=-4?
– user121799
Commented Feb 23, 2018 at 1:38
• @marmot I don't understand because -5 < -4, no? It shouldn't plot anything below -4 because the function would not be defined. Commented Feb 23, 2018 at 1:41
• restrict your domain to -4:4. now the function is drawn to about x=-3.5 because your number of samples is to small than can follows slope of function. or define samples=500 for example. Commented Feb 23, 2018 at 1:46
• @Zarko Thank you very much, it worked! I thought that it would plot like a graphic calculator (or perhaps the calculator uses the same strategy). Commented Feb 23, 2018 at 1:50
• @Johnathan, you actually have math problem: how to draw complex function on real plane :-). see my answer below. Commented Feb 23, 2018 at 1:55

• defining domain=-5:5 require, that pgfplots below -4 draw complex function, what is not possible. so sensible is, that the lower domain limit is -4.
• tangent to function at x=-4 is orthogonal, so the default number of samples is to small that can follows to function change

the simplest solution is

\documentclass[margin=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel=$x$,
ylabel={$y$},
axis lines=center,
%samples=400% for more smuth curve
]
\end{axis}
\end{tikzpicture}
\end{document}


which gives

• Ok, so because we approach a limit (x = -4), we need more points for latex to estimats how the curve behaves at that point, right? Commented Feb 23, 2018 at 2:03
• @Johnathan, yes. you can help pgfplots with splitting plots on two parts: \adplot[domain:-4:-3,5, samples=70,red] {sqrt(x+4)-2};\adplot[domain:-3.5:5, samples=70,red] {sqrt(x+4)-2}; Commented Feb 23, 2018 at 2:10

You do not need any large number of samples if you just realize that what you are drawing is a flipped parabola.

\documentclass[margin=3pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel=$x$,
ylabel={$y$},
axis lines=center]