I want to illustrate how the concept of centrality of data points is hard to generalize in higher dimensions when facing multivariate analysis.

To this end, I begin by explaining and illustrating how natural ordering data points is when working with univariate data. So, I'm trying to draw the following picture in LaTeX using tikz :

enter image description here

What I've done so far is use an R script to generate a .dat file containing the coordinates of random points sampled from a normal distribution and then tried to plot them in the following code:



    \begin{tikzpicture}[only marks]
        \draw plot[mark=*] file {random_numbers.dat};

When compiling my document, I get the following error: Missing number, treated as zero.

Any response to one of the following questions would be very much appreciated !

  1. How do I fix the error ?
  2. How can I color the data points and draw the lines according to the picture I'm trying to reproduce ?
  3. Is there a way to not use a .dat file for this task ? Reading through the documentation of the tikz & pgf packages, it seems that mathematical expressions can be evaluated and plotted but I'm not sure that there are tools to sample observations from a particular distribution.


The .dat file stores the random numbers like this:

"x" "y"
1.52516422413456 0
-1.84552933493078 0
0.599154946213978 0
... ...

where the two columns are separated by spaces.


Using this solution, I've raised another error when I added the following command to my preamble:


I fixed it by adding this after loading the french babel package:

    every picture/.prefix style={
    execute at begin picture=\shorthandoff{?}
  • Welcome to tex.sx. Dec 28, 2022 at 15:37
  • 1
    Welcome. // Can you please edit your post and copy your .dat files content? Format isn‘t clear to me. // Is there a change when you remove the space for file{ ?
    – MS-SPO
    Dec 28, 2022 at 15:55
  • @MS-SPO thanks for you reply. I've tried compiling the same code where the space you mentionned was removed but it didn"t work. I've provided the structure of my .dat file.
    – D.L.
    Dec 28, 2022 at 17:25
  • Thanks for adding. // Recalling chapter 22.4 in the pgfmanual, can it be that you miss the first line like "#Curve 0, 20 points"? ctan.org/pkg/pgf // The example there also ends each line with "i" for the type.
    – MS-SPO
    Dec 28, 2022 at 17:55

1 Answer 1

-1.53   0
-0.95   0
1.04    0
1.58    0
-0.97   0
-0.86   0
0.58    0
0.85    0
0.49    0
-1.37   0
-0.73   0
1.84    0
-0.03   0
-1.04   0
-1.79   0
-0.48   0
-1.37   0
1.17    0
-0.12   0
-1.56   0

\documentclass[tikz, border=1cm]{standalone}
axis lines=center,
axis y line=none,
xmin=-2, xmax=2,
ymin=0, ymax=0,
filter discard warning=false,
\addplot[red,   only marks, x filter/.expression={x<-1.5||x>1.5?x:nan}] table {tmp_random_numbers.dat};
\addplot[blue,  only marks, x filter/.expression={x>-1.5&&x<1.5?x:nan}] table {tmp_random_numbers.dat};
\addplot[green, only marks] coordinates {(0,0)};
\node[pin={[pin distance=0.5cm, pin edge={black}]{centre}}] at (0,0) {};
\node (outliers) at (0,-1.5cm) {outliers};
\draw (-1.9,-2pt) -- (outliers) (1.9,-2pt) -- (outliers);

x axis with different colored dots

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .