5
\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?

3

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}
| improve this answer | |

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.