I have a file with three columns of data, x, y, r.

# x y r
1 1 0.1
2 2 0.5
3 3 1.2

and so on. I'd like to loop over the rows, and draw a circle at x,y with radius r for each row.

I first thought I'd use pgfplots with customized marks, but I couldn't figure out how to pass the radius parameter into the pgfplot mark handler. Then I thought I'd use tikz and pgf directly, but I couldn't figure out how to reference the values for r inside a draw command sequence.

Any thoughts ? I'd be very grateful for some help.


Another option is to use the datatool package.


\DTLforeach*{coordinates}{\x=x, \y=y, \r=r}{\draw (\x,\y) circle (\r);}

where coord.dat is a comma separated file with header row, e.g.


I suggest this solution





\foreach \row in {0,...,\M}{
          \foreach \col in {0,...,\N}{
                 \definecolor{mycolor}{RGB}{\pdfuniformdeviate 256,%
                                            \pdfuniformdeviate 256,%
                                            \pdfuniformdeviate 256}
                 \fill[mycolor,opacity=.5] (\x,\y)circle(\radius cm);

where circles.dat is a file like this

0.75465 2.31086 0.54389
1.43749 1.05065 0.72105
0.18223 1.98603 0.52250
2.30484 1.66463 0.99370
1.36673 1.68386 0.21868
2.18637 3.33167 0.10580
1.70292 0.76932 0.10970
0.64444 0.61346 0.06359
1.29524 0.58225 0.40458
1.35803 2.16296 0.44837
2.54315 2.60982 0.36582
4.72587 0.26478 0.76350
0.41787 0.63615 0.62790
2.83713 0.47686 0.77198
0.94492 0.93983 0.93285
0.11940 2.58221 0.97274
2.42922 0.95893 0.19203
0.90028 3.19658 0.13887
2.29363 1.08943 0.69627
0.66194 2.58925 0.09382

where the first column is x, the second y and the third is the radius.

The result is the following

enter image description here

I hope it helped!

  • @Azoun wow, NICE solution! – cmo Feb 25 '14 at 17:17

Just for completeness, here's a way to control the radius of the plot marks in PGFplots. In general, scatter allows control over the individual plot marks, and scatter src is used to determine where the data for parametrising the marks comes from. scatter src=explicit means that the data is provided in an additional column.

The @pre marker code is code that is executed just before each marker, and can be used to set the radius of the markers. In order to specify the radius in the same units as the rest of the plot, you have to convert "meta data" to scaled axis units using \pgfplotstransformcoordinatex and multiply the result by the length of the unit vector, \pgfplotsunitxlength.


\usepackage{pgfplots, filecontents}

# x y r
1 1 1
3 2 0.5
6 3 2
    scatter src=explicit,
    axis equal,
    only marks,
    xmin=0, xmax=8,
    scatter/@pre marker code/.code={%
      \scope[mark size=\pgfplotsunitxlength*\pgfmathresult]
\addplot file {data.dat};


pgfplots circles with specified radius

  • thanks ! this is very helpful for variations on the above question. I was originally trying to use src=explicit, but wasn't sure how – Suresh Jun 14 '11 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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