# How to add a jitter to a scatter plot with pgfplots

I have the following scatter plot:

\begin{tikzpicture}
\begin{axis}[
enlarge x limits=0.02,
xmin=0,
]
\end{axis}
\end{tikzpicture}


I would like to add a jitter to the marks, i.e. some noise so that one can get a better feeling about the number of marks can would otherwise be placed above each other. In this particular case this is needed for the rightmost values.

-

You can use an x filter to add a bit of random noise to each x coordinate. I've defined a new jitter style that takes an optional argument to control the amount of noise:

\documentclass{standalone}
\usepackage{pgfplots,pgfplotstable}
X   Y
1   2
1   2.1
1   2.05
1   2
1   2.2
1   2.15
1   2.25
1   2.1
1   2.025
1   2.125
1   2.0525
1   2.25
1   2.225
1   2.1525
1   2.2525
1   2.125
}\datatable

\pgfplotsset{
jitter/.style={
x filter/.code={\pgfmathparse{\pgfmathresult+rnd*#1}}
},
jitter/.default=0.1
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[only marks,ymin=0,ymax=4,xmin=0,xmax=5]
\end{axis}
\end{tikzpicture}
\end{document}

-
(+1) Very elegant solution. –  chl Feb 14 '12 at 16:23
But I got this error Package pgfplots Error: Could not read table file ' X Y 1 2 1 2.1 1 2.05 1 2 1 2.2 1 2.15 1 2.25 1 2.1 1 2.025 1 2.125 1 2.0525 1 2.25 1 2.225 1 2.1525 1 2. 2525 1 2.125 '.. –  emab Jan 31 '14 at 8:47
Package pgfkeys Error: I do not know the key '/tikz/jitter' and I am going to ignore it. Perhaps you misspelled it. –  emab Jan 31 '14 at 9:05
@emab: It's working fine for me. Do you get these error messages when running the code exactly as it is posted here? What version of PGFPlots are you running? –  Jake Jan 31 '14 at 9:56
@Jake yes I just copied your code (and changed the document type because I didn't have the standalone style.) pgfplots 2009/02/14 Version 1.2.2 –  emab Feb 1 '14 at 5:27

To add jitter or noise I would rather do it in another program than TeX, however it can be done.

See this code which lets you add noise in the x or y direction.

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[domain=0:10,samples=100,
my x filter/.style={%
x filter/.code={%
\def\tmpx{##1}%
\pgfmathparse{\tmpx > 8 ? \tmpx+rnd*#1 : \tmpx}%
}},
my y filter/.style={%
y filter/.code={%
\def\tmpy{##1}
\pgfmathparse{\tmpy > .75 ? \tmpy+rnd*#1 : \tmpy}
}}]