# Strange behavior of abs(x) in PGF Plots

I have the following code:

\begin{tikzpicture}
\begin{axis}    [
axis lines = {center},
width = {0.6\linewidth},
ylabel = {$y$},
xlabel = {$x$},
ytick distance = {2},
minor y tick num = {1}
]

mark = none, domain= -3:4
]
{abs(x)};
\end{axis}
\end{tikzpicture}

That produces the following image, notice close to x=0that there's a strange "break" on the function's plot. What could be the cause? How do I get rid of that?

I noticed the problem also happens if I use a function with conditions say:

{x<0 ? -x+5 : x-2};

On the area near x=0 a weird break happens.

Not strange, consider where the function value is calculated, by setting mark = x:

The default number of samples is 25, and 25 samples from -3 to 4 gives one sample at -0.0833 and the next at 0.20833. The asymmetry causes the problem you see.

If you set the number of samples and the domain such that you get a sample exactly at zero, you should be fine. With the domain -3:4, samples=8 or samples=15 for example will give you a sample at zero. Alternatively, for this specific case you can set samples at={-3,0,4}:

# Complete code

\documentclass{article}

\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\begin{axis}    [
axis lines = {center},
width = {0.6\linewidth},
ylabel = {$y$},
xlabel = {$x$},
ytick distance = {2},
minor y tick num = {1}
]