我想要一张表格中的热图,并在给定的链接上找到了解决方案 使用 TikZ 绘制热图。@ 发布的答案之一(页面上的最后一个答案)https://tex.stackexchange.com/users/7561/adn生成下表。
上表的代码是
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{pgfplotstable}
\pgfplotstableset{
color cells/.style={
col sep=comma,
string type,
postproc cell content/.code={%
\pgfkeysalso{@cell content=\rule{0cm}{2.4ex}%
\pgfmathsetmacro\y{min(100,max(0,abs(round(##1 * 0.5))))}%
\ifnum##1<0\edef\temp{\noexpand\cellcolor{blue!\y}}\temp\fi%
\ifnum##1>0\edef\temp{\noexpand\cellcolor{red!\y}}\temp\fi%
\pgfmathtruncatemacro\x\y%
\ifnum\x>50 \color{white}\fi%
##1}%
}
}
}
\begin{document}
\begin{table}\caption{Correlation or something}
\centering
\pgfplotstabletypeset[color cells]{
a,b,c,d
50,-300,-200,-100
-20,0,100,200
5,300,40,800
3,30,50,70
}
\end{table}
\end{document}
现在我想将一个表逻辑地分成 6 个大小为 [4x4] 的子表,如下所示
我想要每个子表的热图,根据相应子表的最小值和最大值为每个子表的单元格着色。我希望现在问题更清楚了。
PS:我无法将其作为评论发布。因此将其作为单独的问题提出。
答案1
由于您希望根据子表的最高值和最低值为单元格着色,因此链接问题的第一个答案应该使用。
对于此解决方案,必须手动将表拆分为子表。这可能可以自动完成,但为此编写代码需要几天时间。但有一个优点,您可以colormap
为每个子表单独设置使用。
此外,如果子表之间的列宽不同,则需要使用固定宽度列。
子表设置在表格中,注意要放在括号({...}
)中,否则会出现错误。
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{pgfplotstable}
\usepgfplotslibrary{colormaps}
\usepackage[margin=3cm]{geometry}
\newcolumntype{C}{>{\centering\arraybackslash}p{8mm}} % centered fixed-width-column
% from https://tex.stackexchange.com/a/83865/110842
\pgfplotstableset{
every column/.style={column type=C},
col sep=comma,
/pgfplots/colormap/autumn,
/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}%
\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}
\begin{table}\caption{Correlation or something}
\centering
\begin{tabular}{c@{}c@{}c}
{\pgfplotstabletypeset[color cells={min=-300,max=800}]{
a,b,c,d
50,-300,-200,-100
-20,0,100,200
5,300,40,800
3,30,50,70
}}
&
{\pgfplotstabletypeset[
/pgfplots/colormap/spring, % set different colormap
color cells={min=-300,max=800}]{
e,f,g,h
50,-300,-200,-100
-20,0,100,200
5,300,40,800
3,30,50,70
}}
&
{\pgfplotstabletypeset[color cells={min=-30,max=80}]{
i,j,k,l
5,-30,-20,-10
-2,0,10,20
2,30,4,80
1,3,5,70
}}
\\
{\pgfplotstabletypeset[
every head row/.style={output empty row},
color cells={min=-300,max=800}]{
a,b,c,d
50,-300,-200,-100
-20,0,100,200
5,300,40,800
3,30,50,70
}}
&
{\pgfplotstabletypeset[
every head row/.style={output empty row},
color cells={min=-300,max=800}]{
e,f,g,h
50,-300,-200,-100
-20,0,100,200
5,300,40,800
3,30,50,70
}}
&
{\pgfplotstabletypeset[
/pgfplots/colormap/winter, % set different colormap
every head row/.style={output empty row},
color cells={min=-300,max=800}]{
i,j,k,l
50,-300,-200,-100
-20,0,100,200
5,300,40,800
3,30,50,70
}}
\end{tabular}
\end{table}
\end{document}