# Tikz sqrt function not plotting sqrt(0)

I am trying to plot a function involving sqrt but I don't understand why it is not plotting until sqrt(0).

The blue curve should reach y=0 at x=8 but it stops before, which does not happen with the red curve. Does someone know how to force the blue curve to plot on the full domain between x=0 and x=8?

\documentclass[border=5pt]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}

\begin{axis}[
scale only axis, % To ensure same size on all pictures axis
restrict y to domain=-12:12,
axis x line=center,
axis y line=center,
ticks = none,
samples=150]

\addplot [no markers] coordinates {(12,10)}; % To maintain scale at size without ticks

% Plot curves
\addplot[red, very thick] coordinates {(9.5,0) (9.5,1.98)};

\end{axis}

\end{tikzpicture}

\end{document}


It is not the case that a vertical line with \addplot coordinates .... causes problems. The reason why the blue plot is not complete is that there are rounding errors. It suffices to wrap the argument of the square root in abs.

\documentclass[border=5pt]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}

\begin{axis}[
scale only axis, % To ensure same size on all pictures axis
restrict y to domain=-12:12,
axis x line=center,
axis y line=center,
ticks = none,
samples=150]

\addplot [no markers] coordinates {(12,10)}; % To maintain scale at size without ticks

% Plot curves
\addplot[red, very thick] coordinates {(9.5,0) (9.5,1.98)};

\end{axis}

\end{tikzpicture}

\end{document}


• @JuCa You are right, that is a typo. Thanks! BTW, if you use xmax=12,ymax=10,, you can drop \addplot [no markers] coordinates {(12,10)};. – user194703 May 28 '20 at 16:03

Use a simple plot for the vertical line segment instead. The slope of a vertical line is ±inf which can cause problems with pgfplots.

\documentclass[border=5pt]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}

\begin{axis}[
scale only axis, % To ensure same size on all pictures axis
restrict y to domain=-12:12,
axis x line=center,
axis y line=center,
%ticks = none,
samples=150]

\addplot [no markers] coordinates {(12,10)}; % To maintain scale at size without ticks

% Plot curves
%\addplot[red, very thick] coordinates {(9.5,0) (9.5,1.98)};
\draw [red, very thick](9.5,0) -- (9.5,1.98);

\end{axis}

\end{tikzpicture}

\end{document}


Edit:

Another option to increase the accuracy is to increase the number of samples, as a plus, your curve will look better.

\addplot[blue, very thick, samples=1000, domain=0:8,]{sqrt(20/2.8*(8-x))};

• I have not understood the question...but I like your answer. – Sebastiano May 28 '20 at 15:54
• Thanks for being very supportive ;-) – AboAmmar May 28 '20 at 15:55
• Thank you but this does not solve my problem. It only hides it by changing the red curve which is here only to show the problem. Outside of the minimum example, I need the blue curve to be plotted entirely, not stopped before sqrt(0). For clarification, I would need your axis to be plotted until the origin at (0,0). – JuCa May 28 '20 at 15:56
• OK, I added another option to correct the problem and make the curve much better. – AboAmmar May 28 '20 at 16:14
• Insufficient number of samples makes the curve looks like line segments at the vicinity of (8,0). – AboAmmar May 28 '20 at 16:20