Take the 2-minute tour ×
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 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.

share|improve this question
add comment

3 Answers

up vote 12 down vote accepted

Another option is to use the datatool package.

\documentclass[a4paper]{scrartcl}
\usepackage{datatool,tikz}
\DTLloaddb[noheader=false]{coordinates}{coord.dat}

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

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

x,y,r
2,1,0.1
3,2,0.5
1,3,1.2
share|improve this answer
    
this worked very well for me. Thanks ! –  Suresh Jun 14 '11 at 9:37
add comment

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.

\documentclass{article}

\usepackage{pgfplots, filecontents}

\begin{filecontents}{data.dat}
# x y r
1 1 1
3 2 0.5
6 3 2
\end{filecontents}
\makeatletter
\begin{document}
\begin{tikzpicture}
\begin{axis}[scatter,
    scatter src=explicit,
    axis equal,
    only marks,
    grid=both,
    width=10cm,
    xmin=0, xmax=8,
    scatter/@pre marker code/.code={%
      \pgfplotstransformcoordinatex{\pgfplotspointmeta}%
      \scope[mark size=\pgfplotsunitxlength*\pgfmathresult]
    }
]
\addplot file {data.dat};
\end{axis}
\end{tikzpicture}

\end{document}

pgfplots circles with specified radius

share|improve this answer
    
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
add comment

I suggest this solution

\documentclass{standalone}

\usepackage{tikz}

\usepackage{pgfplotstable}

\begin{document}
\centering
\begin{tikzpicture}
\pgfplotstableread{circles.dat}\table
\pgfplotstablegetrowsof{\table}
\pgfmathsetmacro{\M}{\pgfplotsretval-1}
\pgfplotstablegetcolsof{\table}
\pgfmathsetmacro{\N}{\pgfplotsretval-1}

\foreach \row in {0,...,\M}{
          \foreach \col in {0,...,\N}{
                 \pgfplotstablegetelem{\row}{[index]\col}\of\table
                 \ifnum\col=0
                       \xdef\x{\pgfplotsretval}
                 \fi
                 \ifnum\col=1
                       \xdef\y{\pgfplotsretval}
                 \fi
                 \ifnum\col=2
                       \xdef\radius{\pgfplotsretval}
                 \fi
                 }
                 \definecolor{mycolor}{RGB}{\pdfuniformdeviate 256,%
                                            \pdfuniformdeviate 256,%
                                            \pdfuniformdeviate 256}
                 \fill[mycolor,opacity=.5] (\x,\y)circle(\radius cm);
}
\end{tikzpicture}
\end{document}

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!

share|improve this answer
    
Nice answer ! thanks ! –  Suresh Jun 12 '11 at 0:51
    
@Azoun wow, NICE solution! –  M.P. Feb 25 at 17:17
add comment

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.