我想制作一个热图,只对热图中的两列使用不同的颜色。我可以使用两个不同的 pgfplotstable(即两个不同的热图)来实现这一点。但是,我想知道是否可以只使用一个 pgfplots 表并只更改最后两行的颜色属性来实现这一点。
\documentclass{article}
\usepackage{colortbl}
\usepackage{pgfplots}
\usepackage{filecontents}
\usepackage{pgfplotstable}
\usepackage{array}
\usepackage{graphicx}
\usepackage{longtable}
\usepackage{titlesec}
\usepackage[font=small,compatibility=false]{caption}
\usepackage{fancyhdr}
\usepackage[margin=0.5in]{geometry}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\titleformat{\chapter}
{\normalfont\LARGE\bfseries\color{darkblue}}{\thechapter.}{1em}{}
\pagestyle{fancy}
\fancyhf{}
\lhead{Parent Feedback Report}
\chead{}
\rhead{}
\lfoot{}
\cfoot{}
\rfoot{Kaddy.co}
\addtolength{\topmargin}{.55in}
\newcolumntype{C}{>{\centering\arraybackslash}p{2mm}}
\pgfplotstableset{
/color cells/min/.initial=0,
/color cells/max/.initial=1000,
/color cells/textcolor/.initial=,
color cells/.code={%
\pgfqkeys{/color cells}{#1}%
\pgfkeysalso{%
postproc cell content/.code={%
\begingroup
\pgfkeysgetvalue{/pgfplots/table/@preprocessed cell content}\value
\ifx\value\empty
\endgroup
\else
\pgfmathfloatparsenumber{\value}%
\pgfmathfloattofixed{\pgfmathresult}%
\let\value=\pgfmathresult
\pgfplotscolormapaccess
[\pgfkeysvalueof{/color cells/min}:\pgfkeysvalueof{/color cells/max}]%
{\value}%
{\pgfkeysvalueof{/pgfplots/colormap name}}%
\pgfkeysgetvalue{/pgfplots/table/@cell content}\typesetvalue
\pgfkeysgetvalue{/color cells/textcolor}\textcolorvalue
\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{figure}
\hfill
\resizebox{2.2in}{3.94in}{\pgfplotstabletypeset[
font=\fontsize{6}{15}\selectfont,
/pgfplots/colormap={blackwhite}{color=(white) rgb255=(255,170,0)},
color cells={min=1,max=2323,textcolor=black},
/pgf/number format/fixed,
/pgf/number format/precision=1,
col sep=comma,
row sep=crcr,
]{
SA,A,N\\
1819, 2043, 126\\
2067, 1587, 218\\
986, 2018, 655\\
1257, 2068, 424\\
1722, 1977, 212\\
1322, 2036, 445\\
1264, 2119, 466\\
1205, 1952, 467\\
1855, 1776, 250\\
1638, 2029, 239\\
1953, 1686, 254\\
2020, 1758, 140\\
1835, 1843, 229\\
1371, 2178, 287\\
}}
\resizebox{1.4in}{3.94in}{\pgfplotstabletypeset[
font=\fontsize{6}{15}\selectfont,
/pgfplots/colormap={blackwhite}{color=(white) rgb255=(255,0,0)},
color cells={min=1,max=323,textcolor=black},
/pgf/number format/fixed,
/pgf/number format/precision=1,
col sep=comma,
row sep=crcr,
]{
D,SD\\
24, 3\\
55, 10\\
147, 33\\
61, 3\\
32, 7\\
102, 17\\
60, 11\\
252, 54\\
55, 8\\
58, 8\\
33, 10\\
23, 3\\
41, 9\\
51, 4\\
}}
\end{figure}
\end{document}
答案1
根据pgfplotstable
手册的“从输入数据到输出表:数据处理”一节,这需要通过
如果您需要对特定行应用此类基于内容的操作,则必须编写“if
\pgfplotstablerow
= 以不同方式生成内容”之类的代码。
这似乎相当复杂,需要深入了解 TeX 和 pgfkeys 的工作原理。
这是一种可以解决您的用例并简化基于行索引的内容重新配置的方法:
\documentclass{standalone}
\usepackage{colortbl}
\usepackage{pgfplotstable}
\usepackage{array}
\usepackage{longtable}
\newcolumntype{C}{>{\centering\arraybackslash}p{2mm}}
\pgfplotstableset{
/color cells/min/.initial=0,
/color cells/max/.initial=1000,
/color cells/textcolor/.initial=,
color cells/.code={%
\pgfqkeys{/color cells}{#1}%
\pgfkeysalso{%
postproc cell content/.code={%
\begingroup
\pgfkeysgetvalue{/pgfplots/table/@preprocessed cell content}\value
\ifx\value\empty
\endgroup
\else
\pgfmathfloatparsenumber{\value}%
\pgfmathfloattofixed{\pgfmathresult}%
\let\value=\pgfmathresult
\pgfplotscolormapaccess
[\pgfkeysvalueof{/color cells/min}:\pgfkeysvalueof{/color cells/max}]%
{\value}%
{\pgfkeysvalueof{/pgfplots/colormap name}}%
\pgfkeysgetvalue{/pgfplots/table/@cell content}\typesetvalue
\pgfkeysgetvalue{/color cells/textcolor}\textcolorvalue
\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
}%
}%
},
%
% A new style which allows to modify how cell content is assigned.
% Unfortunately, assignment of cell content cannot be done by
% means of "every row" and its friends -- we have to modify
% 'postproc cell content' to alter it.
% Usage:
% @content options for rows={<row index list>}{<options to apply>}
% where
% <row index list> is a comma-separated list of row indices.
% Negative indices count backwards:
%
% Example:
% @content options for rows={0,3,-1}{<options to apply>}
@content options for rows/.style 2 args={
postproc cell content/.add code={%
\def\isInInputList{0}%
\pgfplotsforeachungrouped \II in {#1} {%
\ifnum\II<0
% allow negative arguments:
\count0=\II
\advance\count0 by\pgfplotstablerows
\edef\II{\the\count0 }%
\fi
%
\ifnum\II=\pgfplotstablerow\relax
\def\isInInputList{1}%
\fi
}%
%\message{checking row \pgfplotstablerow/\pgfplotstablerows\space matches #1...^^J}%
\if1\isInInputList%
\pgfkeysalso{#2}%
\fi
}{},
},
}
\begin{document}
\pgfplotstabletypeset[
font=\fontsize{6}{15}\selectfont,
% defines (and activates) a color map named "special":
/pgfplots/colormap={special}{color=(white) color=(black)},
%
% defines and activates the colormap to use for all cases:
/pgfplots/colormap={CM}{color=(white) rgb255=(255,170,0)},
color cells={min=1,max=2323,textcolor=black},
/pgf/number format/fixed,
/pgf/number format/precision=1,
col sep=comma,
row sep=crcr,
%
% reconfigure a couple of rows:
@content options for rows={3,-1,-2}{/pgfplots/colormap name=special},
]{
SA,A,N\\
1819, 2043, 126\\
2067, 1587, 218\\
986, 2018, 655\\
1257, 2068, 424\\
1722, 1977, 212\\
1322, 2036, 445\\
1264, 2119, 466\\
1205, 1952, 467\\
1855, 1776, 250\\
1638, 2029, 239\\
1953, 1686, 254\\
2020, 1758, 140\\
1835, 1843, 229\\
1371, 2178, 287\\
}
\end{document}
我引入了一个新密钥@content options for rows={<row index list>}{<options to apply>}
,有关详细信息,请参阅内联注释。
请注意,前导@
实际上是不必要的...我添加它是因为我发现这个选项很有用;也许我最终会添加它pgfplotstable
(然后我想重复使用不带 的名称@
)。