2

I have a csv file consisting of two columns corresponding to the x and y coordinates of a point cloud. Since it is possible to label a point in a tikzpicture environment by

\coordinate (1) at (0,0); # from now one simply has to write (1) instead of (0,0)

I was wondering if it is possible to import the points from the csv file and labeling them automatically with their appropriate row number in the file.

1
  • Yes that's what pgfplots does. You can also use plot from TikZ – percusse Nov 3 '17 at 0:18
4

Here, I use readarray to absorb and compose the data. I label the coordinates as R\i where \i is the local row number.

\documentclass[border=3mm,tikz]{standalone}
\usepackage{readarray,filecontents}
\begin{filecontents*}{mydata}
-4,16
-3,9
-2,4
-1,1
-.5,.25
0, 0
.5,.25
1, 1
2, 4
3, 9
4,16
\end{filecontents*}
\begin{document}
\readarraysepchar{,}
\readdef{mydata}\myrecorddata
\readarray\myrecorddata\xypts[-,2]
\begin{tikzpicture}
\def\thedamnedline{}% INITIALIZE
\foreach\i in {1, ..., \xyptsROWS}{
  \coordinate[label=\textcolor{red}{R\i}](R\i) at (\xypts[\i,1],\xypts[\i,2]);% DEFINE/LABEL
  \fill[red] (R\i) circle (2pt);% ADD CIRCLE AT EACH COORDINATE
  \ifnum\i>1\relax\xdef\thedamnedline{\thedamnedline -- }\fi% COMPOSE INTERCOORDINATE LINKS
  \xdef\thedamnedline{\thedamnedline (R\i) }% COMPOSE COORDINATES SEQUENTIALLY FOR LINE
}
\draw\thedamnedline;% DRAW \thedamnedline (PREVIOUSLY COMPOSED)
\end{tikzpicture}
\end{document}

enter image description here

0

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.