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.
\begin{tikzpicture}
  \begin{axis}
    \addplot coordinates {
      (1,1) (2,4) (3,9) (4,16)
    };
    \draw (axis cs:0,0) -- (axis cs:3,9);
  \end{axis}
\end{tikzpicture}

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?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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:

\begin{tikzpicture}
  \begin{axis}
    \pgfplotstableread{data.dat}{\mytable}          %% load table
    \addplot table[x=xcol,y=ycol] {\mytable};
    \pgfplotstablegetelem{2}{xcol}\of{\mytable}     %% get n-th x
    \pgfmathsetmacro{\xn}{\pgfplotsretval}
    \pgfplotstablegetelem{2}{ycol}\of{\mytable}     %% get n-th y
    \pgfmathsetmacro{\yn}{\pgfplotsretval}
    \path[draw] (axis cs:0,0) -- (axis cs:\xn,\yn); %% draw line
  \end{axis}
\end{tikzpicture}
share|improve this answer

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.