我有一张热图表,想将行和列的标题分别垂直放置在表格的左侧和水平放置在表格的顶部(参见 MWE 的第一个表格),但是当我这样做时,我发现没有办法重置第二列的样式,就像我在第二个表格中对第一列进行重置一样。
我尝试按名称来调用它,因为我查找时找到的每个答案似乎都表明了这一点,但似乎不起作用。
梅威瑟:
\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\usepackage{colortbl}
\usepackage{multirow}
\pgfplotsset{compat=1.16}
\begin{filecontents*}{test.csv}
empty ,title , 1 , 2 , 3 , 4 , 5
,1 , 93.10344828 , 97.64705882 , 99.0990991 , 97.89473684 , 91.78082192
,2 , 76.72413793 , 80 , 84.68468468 , 95.78947368 , 76.02739726
,3 , 91.37931034 , 80 , 95.4954955 , 86.31578947 , 97.26027397
,4 , 78.44827586 , 89.41176471 , 93.24324324 , 93.68421053 , 89.7260274
\end{filecontents*}
\begin{filecontents*}{test2.csv}
title , 1 , 2 , 3 , 4 , 5
1 , 93.10344828 , 97.64705882 , 99.0990991 , 97.89473684 , 91.78082192
2 , 76.72413793 , 80 , 84.68468468 , 95.78947368 , 76.02739726
3 , 91.37931034 , 80 , 95.4954955 , 86.31578947 , 97.26027397
4 , 78.44827586 , 89.41176471 , 93.24324324 , 93.68421053 , 89.7260274
\end{filecontents*}
%%%%%%%%% Heatmap configuration, not my work %%%%%%%%%%%%%%%%%%%%
\pgfplotstableset{
/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 http://tex.stackexchange.com/questions/12668/where-do-i-start-latex-programming/27589#27589
\toks0=\expandafter{\typesetvalue}%
\xdef\temp{%
\noexpand\pgfkeysalso{%
@cell content={%
\noexpand\cellcolor[rgb]{\pgfmathresult}%
\noexpand\definecolor{mapped color}{rgb}{\pgfmathresult}%
\ifx\textcolorvalue\empty
\else
\noexpand\color{\textcolorvalue}%
\fi
\the\toks0 %
}%
}%
}%
\endgroup
\temp
\fi
}%
}%
}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\pgfplotstabletypeset[%
col sep=comma,
%Heatmap stuff
color cells={min=50,max=100,textcolor=black},
/pgfplots/colormap={redyellowgreen}{rgb255=(255,0,0) color=(yellow) rgb255=(0,255,0)},
/pgf/number format/fixed,
/pgf/number format/precision=1,
%Titles and lines
every head row/.style={
before row={&&\multicolumn{5}{c}{\textbf{top}} \\ \cmidrule(lr){3-7}},
after row/.add ={ \cmidrule(lr){2-7}
\multirow{4}{*}{\rotatebox{90}{left}}
}%
},
every last row/.style={after row=\cmidrule[\heavyrulewidth](lr){2-7}},
%should remove the heatmapping from the titles column
columns/title/.style={column name=,reset styles,string type,column type={l}},%
every first column/.style={column name=,reset styles,string type,column type={l}}%
]{test.csv}
\bigskip
\pgfplotstabletypeset[%
col sep=comma,
%Heatmap stuff
color cells={min=50,max=100,textcolor=black},
/pgfplots/colormap={redyellowgreen}{rgb255=(255,0,0) color=(yellow) rgb255=(0,255,0)},
/pgf/number format/fixed,
/pgf/number format/precision=1,
%Titles and lines
every head row/.style={
before row={&\multicolumn{5}{c}{\textbf{top}} \\ \cmidrule(lr){2-6}},
after row/.add ={ \midrule
%The multirow merges with the first column if uncommented
%\multirow{4}{*}{\rotatebox{90}{left}}
}%
},
every last row/.style={after row=\toprule},
%
%removes the heatmapping from the titles column
every first column/.style={column name=,reset styles,string type,column type={l}}%
]{test2.csv}
\end{document}
答案1
好的,感谢这篇文章:自定义 pgfplotstable 中每行的第一列经过大量的实验,我发现表格第一行的空格会破坏通过
columns/<name>/.style=<...>
删除这些就可以解决所有问题。
希望这对某人有用。