Pgfplots:将 headrow 转置为第一列:带有颜色图/热图的混淆矩阵

Pgfplots:将 headrow 转置为第一列:带有颜色图/热图的混淆矩阵

我的问题基于这个问题的代码: 原始问题/答案

我想要实现的目标:

  1. 我想使用 pgfplots 来构建一个表格,其中标题行会自动添加为表格的第一列(类似于标题列)。
  2. 不太重要:我想在表格下方和最后一行之后添加旋转的标题行/列,以解释在这些轴上看到的内容。

为了澄清起见,我准备了一张小图:

顶部:MWE/我拥有的。底部:我想要的

我的主要问题:

我的主要问题是如何通过索引访问列标题名称,如代码中所见。

梅威瑟:

\documentclass[10pt,twocolumn,letterpaper]{article}

\usepackage[table]{xcolor}
\usepackage{pgfplotstable}

%for confusion table

\pgfplotstableset{
precision=1,
fixed,
fixed zerofill,
/color cells/min/.initial=0,
/color cells/max/.initial=1000,
/color cells/textcolor/.initial=,
%
% Usage: 'color cells={min=<value which is mapped to lowest color>, 
%   max = <value which is mapped to largest>}
color cells/.code={%
    \pgfqkeys{/color cells}{#1}% 
    \pgfkeysalso{%
        postproc cell content/.code={%
            %
            \begingroup
            %
            % acquire the value before any number printer changed
            % it:
            \pgfkeysgetvalue{/pgfplots/table/@preprocessed cell content}\value
            \ifx\value\empty
                \endgroup
            \else
            \pgfmathfloatparsenumber{\value}%
            \pgfmathfloattofixed{\pgfmathresult}%
            \let\value=\pgfmathresult
            %
            % map that value:
            \pgfplotscolormapaccess
                [\pgfkeysvalueof{/color cells/min}:\pgfkeysvalueof{/color cells/max}]
                {\value}
                {\pgfkeysvalueof{/pgfplots/colormap name}}%
            % now, \pgfmathresult contains {<R>,<G>,<B>}
            % 
            % acquire the value AFTER any preprocessor or
            % typesetter (like number printer) worked on it:
            \pgfkeysgetvalue{/pgfplots/table/@cell content}\typesetvalue
            \pgfkeysgetvalue{/color cells/textcolor}\textcolorvalue
            %
            % tex-expansion control
            % see https://tex.stackexchange.com/questions/12668/where-do-i-start-latex-programming/27589#27589
            \toks0=\expandafter{\typesetvalue}%
            %%% my try to replicate the header row starts here
            \xdef\III{0}
             \ifnum\III=\pgfplotstablecol\relax
                 \def\addedContent{ \pgfplotstablecolname  &}%
             \else
                 \def\addedContent{}%
             \fi
            %%% my try ends here: problem, I dont have access to the column header names
            \xdef\temp{%
                \noexpand\pgfkeysalso{%
                    @cell content={%
                        \addedContent
                        \noexpand\cellcolor[rgb]{\pgfmathresult}%
                        \noexpand\definecolor{mapped color}{rgb}{\pgfmathresult}%
                        \ifx\textcolorvalue\empty
                        \else
                            \noexpand\color{\textcolorvalue}%
                        \fi
                        \the\toks0 %
                    }%
                }%
            }%
            \endgroup
            \temp
            \fi
        }%
    }%
},
  every head row/.style={
 before row=&,
  },
  every first column/.style={
      column type/.add={c}{},
  },
}



\begin{document}
\begin{table}
\caption{What I have}
\centering
\setlength{\tabcolsep}{1pt}
\pgfplotstabletypeset[
    /pgfplots/colormap={CM}{color=(white) rgb255=(255,170,0)},
    color cells={min=0,max=100,textcolor=black},
    col sep=space,row sep=\\
]{A E F I \\
98.07692 9.25926 2.33516 0.00000\\
0.00000 74.07407 3.02198 0.00000\\
0.00000 0.00000 58.79121 0.00000\\
0.00000 3.70370 1.64835 100.00000\\
}
\end{table}
\end{document}

答案1

您可以更换

    %%% my try to replicate the header row starts here
     \xdef\III{0}
     \ifnum\III=\pgfplotstablecol\relax
         \def\addedContent{ \pgfplotstablecolname  &}%
     \else
         \def\addedContent{}%
     \fi
    %%% my try ends here: problem, I dont have access to the column header names

经过

 \ifnum0=\pgfplotstablecol\relax
     \pgfplotstablegetcolumnnamebyindex\pgfplotstablerow\of\pgfplotstabletypesetfile@opt@@\to\tempcolname
     \def\addedContent{ \tempcolname  &}%
 \else
     \def\addedContent{}%
 \fi

其中,\pgfplotstablegetcolumnnamebyindex定义如下:

% Defines #3 to be the column name of the column with index '#1'.
%
% #1: a column index (starting with 0)
% #2: a loaded table structure
% #3: an output macro name.
\def\pgfplotstablegetcolumnnamebyindex#1\of#2\to#3{%
  \pgfplotslistselect#1\of#2\to#3\relax
}%

并且\pgfplotstabletypesetfile@opt@@是内联表格的临时名称。如果您恰好想要排版已加载的表格,请用您正在排版的表格替换此标记。

剩下的问题算是\tikzmark练习。所以我就留给你们了。

相关内容