0

I am trying to access values from a file one by one, and using the following code to do it:

\nextgroupplot[
    width=0.3*\plotwidth,
]
    \foreach \i in {1,...,10}{
        \foreach \j in {0,...,9}{
            \ifnum\i>\j
                \pgfplotstablegetelem{\i}{\j}\of{\first};
                %\let\param\pgfplotsretval;
                %\node at (axis cs:1.5, 0.5) {\pgfplotsretval};
                \addplot [draw=none, mark=, fill=blue, fill opacity=1] coordinates {(0.5+\j, -0.5+\i)} node[pos=0.1] {\pgfplotsretval};
            \else
            \fi         
        }
}

Unfortunately, it does not work, and I fail to see the problem. Interestingly, when I use

\node at (axis cs:1.5, 0.5) {\pgfplotsretval};

it works -- almost, since it only gives me the first digit of the numbers in the table.

P.S. By the way, I am not using this to just put numbers in a grid. It is a debugging step, I will eventually use the values to draw colours from a \colormap.

MWE:

\begin{filecontents*}{data.csv}
1   2   3   4   5   6   7   8   9   10
0.1 0   0   0   0   0   0   0   0   0
0.05625 0.012499999999999999    0   0   0   0   0   0   0   0
0.05185185185185186 0.008101851851851851    0.003703703703703704    0   0   0   0   0   0   0
0.05078125  0.007031249999999999    0.0026331018518518517   0.0015625   0   0   0   0   0   0
0.0504  0.00665 0.002251851851851852    0.00118125  0.0008000000000000001   0   0   0   0   0
0.05023148148148148 0.006481481481481481    0.0020833333333333333   0.0010127314814814816   0.0006314814814814816   0.00046296296296296303  0   0   0   0
0.05014577259475219 0.006395772594752186    0.0019976244466040384   0.0009270225947521866   0.0005457725947521867   0.0003772540762336681   0.0002915451895043732   0   0   0
0.050097656250000004    0.006347656249999999    0.001949508101851852    0.00087890625   0.00049765625   0.0003291377314814815   0.00024342884475218656  0.00019531249999999998  0   0
0.050068587105624146    0.006318587105624142    0.0019204389574759945   0.0008498371056241427   0.00046858710562414275  0.00030006858710562417  0.00021435970037632926  0.00016624335562414266  0.00013717421124828533  0
0.050050000000000004    0.006299999999999999    0.0019018518518518518   0.0008312500000000001   0.0004500000000000001   0.0002814814814814815   0.00019577259475218658  0.00014765625   0.00011858710562414267  0.0001
\end{filecontents*}

\documentclass[margin=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usepackage{pgfplotstable}
\usepgfplotslibrary{groupplots}

\definecolor{color1}{RGB}{202,0,32}
\definecolor{color2}{RGB}{244,165,130}
\definecolor{color3}{RGB}{146,197,222}
\definecolor{color4}{RGB}{5,113,176}

\def \plotwidth {510.0pt}

\begin{document}
\pgfplotstableread[col sep=tab]{data/first_order.dat}{\first}

\begin{tikzpicture}
\begin{groupplot}[
    group style = {
        group size = 3 by 1,
        horizontal sep = 0pt,
        yticklabels at=edge left,
    },
    height = 150pt,
    width = \plotwidth,
    xmin=0, xmax=10,
    ymin=0, ymax=10,
    xtick = {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5},
    ytick = {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5},
]
\nextgroupplot[
    width=0.3*\plotwidth,
]
    \foreach \i in {1,...,5}{
        \foreach \j in {0,...,3}{
            \ifnum\i>\j
                \pgfplotstablegetelem{\i}{\j}\of{\first};
                \addplot [draw=none, mark=, fill=blue, fill opacity=1] coordinates {(0.5+\j, -0.5+\i)} node[pos=0.1] {\pgfplotsretval};
            \else
            \fi         
        }
    }
\nextgroupplot[
    width=0.3*\plotwidth
]
\nextgroupplot[
    width=0.3*\plotwidth
]
  \end{groupplot}
\end{tikzpicture}
\end{document}
3
  • I suspect \pgfplotsretval is being used internally by \node. A full Minimal Working Example would help. Dec 13, 2016 at 4:06
  • Hi, I believe this is the problem, yes. Though I have no idea how to solve it. I have appended a MWE.
    – John
    Dec 13, 2016 at 9:19
  • You can examine the newly revised readarray package for getting file data into accessible array structures. Jan 12, 2017 at 16:34

1 Answer 1

1

I had to make a number of changes to get your MWE to run (tabs got converted to spaces). I also got rid of all the groupplots, as getting one plot to work was difficult enough. I'm not sure what the \addplots were supposed to do besides position the nodes.

Anyway, the solution was to use \pgfplotsextra for the entire loop. Otherwise by the time the node was drawn, \i was always 5 and \j was always 3. See also \pgfplotsforeachungrouped and \pgfplotsinvokeforeach.

\begin{filecontents*}{data.csv}
1,  2,  3,  4,  5,  6,  7,  8,  9,  10
0.1, 0,  0,  0,  0,  0,  0,  0,  0,  0
0.05625, 0.012499999999999999,   0,  0,  0,  0,  0,  0,  0,  0
0.05185185185185186, 0.008101851851851851,   0.003703703703703704,   0,  0,  0,  0,  0,  0,  0
0.05078125, 0.007031249999999999,   0.0026331018518518517,  0.0015625,  0,  0,  0,  0,  0,  0
0.0504, 0.00665, 0.002251851851851852, 0.00118125, 0.0008000000000000001,  0,  0,  0,  0,  0
0.05023148148148148, 0.006481481481481481,  0.0020833333333333333,  0.0010127314814814816,  0.0006314814814814816,  0.00046296296296296303, 0, 0, 0, 0
0.05014577259475219, 0.006395772594752186,  0.0019976244466040384,  0.0009270225947521866,  0.0005457725947521867,  0.0003772540762336681,  0.0002915451895043732, 0,  0,  0
0.050097656250000004, 0.006347656249999999, 0.001949508101851852, 0.00087890625, 0.00049765625, 0.0003291377314814815, 0.00024342884475218656, 0.00019531249999999998, 0, 0
0.050068587105624146, 0.006318587105624142, 0.0019204389574759945, 0.0008498371056241427, 0.00046858710562414275, 0.00030006858710562417, 0.00021435970037632926, 0.00016624335562414266, 0.00013717421124828533, 0
0.050050000000000004, 0.006299999999999999, 0.0019018518518518518, 0.0008312500000000001, 0.0004500000000000001, 0.0002814814814814815, 0.00019577259475218658, 0.00014765625, 0.00011858710562414267, 0.0001
\end{filecontents*}

\documentclass[margin=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usepackage{pgfplotstable}
\usepgfplotslibrary{groupplots}

\definecolor{color1}{RGB}{202,0,32}
\definecolor{color2}{RGB}{244,165,130}
\definecolor{color3}{RGB}{146,197,222}
\definecolor{color4}{RGB}{5,113,176}

\def \plotwidth {510.0pt}

\begin{document}
\pgfplotstableread[col sep=comma]{data.csv}{\first}
%\pgfplotstabletypeset{\first}

\begin{tikzpicture}
\begin{axis}[
    height = 150pt,
    width = 5in,
    xmin=0, xmax=10,
    ymin=0, ymax=10,
    xtick = {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5},
    ytick = {0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5},
]
\pgfplotsextra{
    \foreach \i in {1,...,5}{
        \foreach \j in {0,...,3}{
            \ifnum\i>\j\relax
                \pgfplotstablegetelem{\i}{\j}\of\first
                \node at (axis cs: 0.5+\j, -0.5+\i) {\pgfplotsretval};
            \else
            \fi         
        }
    }
}
\end{axis}
\end{tikzpicture}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .