# Heatmap in table

I want a heat map in a table and found a solution on a given link Drawing heatmaps using TikZ. One of the answers (last answer on the page) posted by @https://tex.stackexchange.com/users/7561/adn produces the following table.

The code for the above table is

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


Now I want a table logically divide into say 6 sub tables of size [4x4] as shown below

I want a heatmap for each subtable which colors the cells of each sub table according to min and max values the corresponding subtable. I hope the question is more clear now.

P.S. I was unable to post it as a comment. Therefore asking as a separate question.

• It is good that you did not post a new question as a comment. ;-) Could you please provide us with the code which you tried? – user121799 Jul 11 '18 at 17:57
• @marmot The code is added which I found at link mentioned in the question. – Jitendra Jul 11 '18 at 18:03
• The code you posted asigns the color only based on the value of the current cell. Maximum and minimum of all the values in the table are not taken into account. For this, dividing the table into subtables wouldn't make any difference. So please elaborate on what you want to achieve. – Mike Jul 11 '18 at 21:36
• I don't understand the question. What does 'show the heat map accordingly' mean? – cfr Jul 11 '18 at 23:07
• @Mike I have edited the question and hopefully, it is more clear now. Looking for help. – Jitendra Jul 12 '18 at 5:15

Since you want to color the cells based on the highest and lowest value of the sub-tables, the first answer of the linked question should be used.

For this solution, the table must be split into sub-tables manually. This could probably be done automatically, but writing the code for this would take days. But there is an advantage, you can set the used colormap individually for each sub-table.

Also, it will be necessary to use a fixed-width-column, if the column width differs between sub-tables.

The sub-tables are set in a table. Note that they are put in braces ({...}), otherwise there will be errors.

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