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


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.


% Define new column and row counters

% 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
  \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
% Redefine the typeset code to call \tabnode and reset counters as needed
  typeset cell/.code={
    \pgfkeyssetvalue{/pgfplots/table/@cell content}{\tabnode{#1}
    \pgfkeyssetvalue{/pgfplots/table/@cell content}{\tabnode{#1}&}%

% 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 \\

% 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
\hfill %
% Plot the data. 'clip=false' stops the path being clipped at the axis boundary
\begin{tikzpicture}[remember picture]
\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);


drawing lines between table and plot

Related Question