\addplot coordinates {
      (1,1) (2,4) (3,9) (4,16)
    \draw (axis cs:0,0) -- (axis cs:3,9);

I can easily draw a line between two points in a coordinate system. Here, I got a few coordinates of y=x². I can draw a line from the origin of the coordinate system (0,0) to (3,9), which is laying on the plot. (3,9) is the 3rd coordinate of the plot. Would it be possible to write somthing like

\draw (axis cs:0,0) -- (plotcs:3);

to access the coordinates of the 3rd tuple in a plot?


I put the table into a file data.dat:

% y = x^2
xcol   ycol
1      1
2      4
3      9
4     16

And could access the coordinates via pgfplotstable package:

    \pgfplotstableread{data.dat}{\mytable}          %% load table
    \addplot table[x=xcol,y=ycol] {\mytable};
    \pgfplotstablegetelem{2}{xcol}\of{\mytable}     %% get n-th x
    \pgfplotstablegetelem{2}{ycol}\of{\mytable}     %% get n-th y
    \path[draw] (axis cs:0,0) -- (axis cs:\xn,\yn); %% draw line

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.