Tell me more ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}

http://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.

share|improve this question

2 Answers

up vote 7 down vote accepted

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}
share|improve this answer
(+1) Very elegant solution. – chl Feb 14 '12 at 16:23

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.