[Tex/LaTex] Pointing from Data in a pgfplotstable to a pgfplot

pgfplotspgfplotstable

Are there PGF coordinates available for elements in a pgfplots table? Groupplots allow to address plots via (group c2r1.center) (column 2, row1).

\pgfplotstabletypeset[col sep=&,row sep=\\,sci zerofill]{
  colA & colB & colC \\
  11   & 12   & 13   \\
  21   & 22   & 23   \\
}

With such coordinates I could draw an arrow from data in a pgfplotstable to its representation in a plot.

Best Answer

Not out of the box, because all pgfplotstable does is typeset a normal tabular. You can make \pgfplotstabletypeset wrap the cell contents in TikZ nodes, however, similar to what is done by hand in the answer to How to draw lines around multiple table cells.

Below is the code including an example of how to use it. It redefines the cell contents of the tables generated with \pgfplotstabletypeset to contain nodes with the naming scheme <table name>-<row>-<col>, which is the same scheme also used by TikZ matrix command.

Note this needs two compile runs to position everything correctly, which is always the case when using TikZ' remember picture option.

\documentclass{article}
\usepackage{pgfplotstable}
\begin{document}

% Define new column and row counters
\newcounter{colcount}
\newcounter{rowcount}

% Command for wrapping cell contents in TikZ nodes with name <table name>-<row>-<col>
% 'remember picture' is necessary to make the node positions available in other tikzpicture environments
\newcommand\tabnode[1]{%
  \addtocounter{colcount}{1}%
  \tikz[remember picture,baseline,inner sep=0pt]
    \node (\pgfkeysvalueof{/pgfplots/table/name}-\arabic{rowcount}-\arabic{colcount}) {#1};
}

% New key for referencing tables typeset with pgfplotstabletypeset
\pgfplotstableset{name/.initial={table}}
% Redefine the typeset code to call \tabnode and reset counters as needed
\pgfplotstableset{
  typeset cell/.code={
  \ifnum\pgfplotstablecol=\pgfplotstablecols
    \pgfkeyssetvalue{/pgfplots/table/@cell content}{\tabnode{#1}
      \addtocounter{rowcount}{1}
      \setcounter{colcount}{0}\\
    }%
  \else
    \pgfkeyssetvalue{/pgfplots/table/@cell content}{\tabnode{#1}&}%
  \fi
}
}


% Example of use: Read table and save to a macro.
% This step is not necessary, you could also provide the table inline to \pgfplotstabletypeset
\pgfplotstableread[col sep=&,row sep=\\]{
  colA & colB & colC & Column D \\
  11   & 12   & 13  & 9 \\
  21   & 22   & 23  & 15 \\
  17 & 10 & 19 & 20 \\
}\mytable


\begin{minipage}{0.4\textwidth}
% Typeset the table as usual.
% You can provide an optional name, otherwise the nodes will be called tab-<row>-<col>
\pgfplotstabletypeset[col sep=&,row sep=\\,sci zerofill,name=table]\mytable
\end{minipage}%
\hfill %
\begin{minipage}{0.4\textwidth}
% Plot the data. 'clip=false' stops the path being clipped at the axis boundary
\begin{tikzpicture}[remember picture]
\begin{axis}[clip=false,width=5cm]
\addplot table \mytable;
% Draw a connection line. Needs the 'overlay' option
\draw[overlay,red] (table-2-2) -- (axis cs:21,22);
\draw[overlay,red] (table-3-2) -- (axis cs:17,10);
\end{axis}
\end{tikzpicture}
\end{minipage}

\end{document}

drawing lines between table and plot

Related Question