# PGFplot producing “noise” (incorrect graph)

Update: This has problems if the samples are one of these: {19,21,23,28,30,31,32,34,36,38,39}, so I think something else is going on.

I can get this example to work fine if I reduce the number of samples to 40 or below. Otherwise, this results in an incorrect graph. I am wondering if this seemingly huge round off problem might indicate that there is some other underlying problem with the math engine. So, is this a know issue, and I just have to make sure that I adjust the number of samples based on what is being plotted?

The MWE example below produces the following output as it stands now, when it should just be a straight line.

\documentclass{article}
\usepackage{pgfplots}
\usepackage{amsmath}

\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin=-4,xmax=+0, ymin=-0.5,ymax=4.5]

samples=100] % Fine is sample < 42, and NOT {19,21,23,28,30,31,32,34,36,38,39}
{(3*x + abs(x))/x}
node [midway, above right] {$y=\frac{3x + |x|}{x}$};

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


## Update 2011-11-03: Am not able to reproduce this behavior with the current TeXLive2011 release, so problem is definitely fixed.

-
I get the same output with a freshly updated TeXLive. –  Martin Scharrer May 18 '11 at 22:58
on a side note: I have seen artefacts like the one at x=-1.5 in "real life" as a result of over-sampling of a data acquisition card run at high frequencies... –  Martin H May 18 '11 at 23:30
Since the problem occurs at even small sample sizes like 19, I don't think this is a problem with just over sampling. –  Peter Grill May 19 '11 at 0:02
It works without errors in the development version, but I wasn't able to find the commit that fixed this. –  Jake May 19 '11 at 5:19

Seems as if this is (was) a bug in pgf/tikz: on my machine, I get a straight line.

I can confirm that this happens with pgf 2.10 and the stable version(s) of pgfplots.

It vanishes for pgf 2.00 and/or for pgf CVS.

I have just uploaded an unstable version of pgfplots which incorporates the pgf 2.10 bugfix: your example will work with the unstable pgfplots version from http://pgfplots.sourceforge.net/ and pgf 2.10 as well.

So, to solve the problem in your application, I see the following approaches:

1. use the unstable of pgfplots (which is pretty stable now)
2. use pgf CVS

Note: Installing pgf CVS or pgf 2.00 manually is more complicated than installing pgfplots since pgfplots comes in a TDS structure (which is compatible with local texmf installation directories). The pgf versions need to be adjusted manually before TeX distributions accept them as TDS.

-

It seems that tikz has some problems with the calculation. Probably it is a rounding error?

As a work around you could -shell-escape to gnuplot and let gnuplot do the calculation

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}