17

I have the following scatter plot:

\begin{tikzpicture}
  \begin{axis}[
    enlarge x limits=0.02,
    xmin=0,
  ]
    \addplot+[only marks,mark size=1pt] table[x=rmsd,y=seq_score_min] {\featuretable};
  \end{axis}
\end{tikzpicture}

https://i.stack.imgur.com/fG8tJ.png

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.

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
Fabian
  • 809

3 Answers3

16

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}
\pgfplotstableread{
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]
\addplot +[jitter=0.3] table {\datatable};
\end{axis}
\end{tikzpicture}
\end{document}
Jake
  • 232,450
  • 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 '.. – orezvani Jan 31 '14 at 08:47
  • Package pgfkeys Error: I do not know the key '/tikz/jitter' and I am going to ignore it. Perhaps you misspelled it. – orezvani Jan 31 '14 at 09:05
  • 1
    @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 09: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 – orezvani Feb 01 '14 at 05:27
  • Be mindful, if you are using any other style that makes use of x filter, like for example Jake's very own and very useful discard if not, you may end up with no jitter unless you combine both definitions. – Matthias Arras Dec 29 '20 at 13:40
  • Is there a way to make the points have positive and negative jitter? For my plot, all points wander to the right, which looks a bit odd. – fuj36840 Oct 16 '21 at 12:11
5

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}
        }}]
    \addplot[blue,my x filter=.5] function {sin(x)};
    \addplot[red,my y filter=.1] function {cos(x)};
  \end{axis}
\end{tikzpicture}
\end{document}

This will produce (not exactly, due to the rand).

example

nickpapior
  • 17,275
2

Lacking permission to comment Jack's answer I make this a new one.

The given code will only add positive jitter, so points are offset to the right of their actual value. If you prefer a symmetric jitter, use this instead:

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

rnd Generates a pseudo-random number between 0 and 1 with a uniform distribution. (from the PGF manual), so we simply have to subtract 0.5 to move it between -0.5 and +0.5. Note that you first adjust the random part, then apply the factor.

TexMax
  • 93