\pgfplotstablegetelem 问题

\pgfplotstablegetelem 问题

我正在尝试逐个访问文件中的值,并使用以下代码来执行此操作:

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

不幸的是,它不起作用,我没能发现问题所在。有趣的是,当我使用

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

它几乎可以起作用,因为它只给我表中数字的第一位数字。

PS 顺便说一句,我并不是用这个来将数字放入网格中。这是一个调试步骤,我最终将使用这些值从 \colormap 中绘制颜色。

梅威瑟:

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

答案1

我必须进行一些更改才能运行您的 MWE(制表符被转换为空格)。我还删除了所有组图,因为让一个图工作起来已经够困难了。除了\addplots定位节点之外,我不确定它们应该做什么。

无论如何,解决方案是在整个循环中使用\pgfplotsextra。否则,到绘制节点时,\i始终为 5 并且\j始终为 3。另请参阅\pgfplotsforeachungrouped\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}

相关内容