2

While there are already several answers discussing adding spacing in tables, I haven't found any that discuss adding space between cells. In particular, I'm using colored cells and would like to add a bit of white space between the color. I'm trying to create a heatmap table that looks similar to this Seaborn (Python plot styling package) example. I'm able to create the colored cells using the pgfplotstable code from this answer. Then just to make the sizes of the cells a bit nicer, I use the cellspace package. However, it's then adding in the white space between the cells that's giving me some trouble.

All the table spacing solutions I've found only seem to extend the cells themselves. I also considered adding white table rules between all the colored cells, but as I'm using the booktabs package the vertical rules are not connected and they don't create the gaps between cells that I'm looking for (also, I'm using the regular booktab rules for the none colored part of the cells [see example below], so I would prefer not to remove booktabs on this table).

Is there a reasonable way to add this spacing between the colored cells? (If the solution allows me to change the height of the colored cells without changing the height of the header cells that would be extra fantastic, though is not required.) Below is the setup of the mostly complete table code without the desired space between cells that I currently have (though I'm fine with a completely different approach if it works). Thank you for your time!

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\usepackage{cellspace}
\usepackage{siunitx}
\usepackage{xcolor}
\usepackage{colortbl}

\begin{document}
\newlength{\cellspacelimit}
\setlength{\cellspacelimit}{5pt}
\setlength{\cellspacetoplimit}{\cellspacelimit}
\setlength{\cellspacebottomlimit}{\cellspacelimit}
\pgfplotstableset{
    /color cells/min/.initial=0,
    /color cells/max/.initial=100,
    /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
            }%
        }%
    },
    every column/.style={column type={Cc}},
    every first column/.style={reset styles, column type={l}, string type},
    every head row/.style={before row=\toprule, after row=\midrule},
    every last row/.style={after row=\bottomrule},
}

\begin{table}
    \centering
    \pgfplotstabletypeset[
    color cells={min=-300,max=800},
    col sep=comma,
    ]{
        {a},{b},{c},{d}
        {Trial 1},-300,-200,-100
        {Trial 2},0,100,200
        {Trial 3},300,40,800
        {Trial 4},30,50,70
    }
\end{table}
\end{document}

Heatmap Image

3

It was hard to find out the solution since your table is not a regular one. It was necessary to look for the appropiate commands for horizontal and vertical rules to create the illusion of separation of the colored cells in pgfplotstable. By the way, I have no idea why colors in my output are different from yours.

enter image description here

I refered to this answer to change the width and color of rules with the following codes:

For vertical rules, it is necessary to write in preamble

\newcolumntype{?}{!{\color{white}\vrule width 1pt}}

(to define the vertical separation of columns) and for horizontal rules, it is necessary to make some changes in columns and rows specifications part. (I defined the rules width of 1pt, but can change it at will.)

every column/.style={column type={Cc?}},    % <-- Note symbol `?` of \newcolumntype defined
every first column/.style={reset styles, column type={l}, string type},
every head row/.style={before row=\toprule, after row=\midrule},
after row={\noalign{\global\arrayrulewidth=1pt}\arrayrulecolor{white}\hline}, % <-- Added
every last row/.style={after row=\arrayrulecolor{black}\bottomrule}, % <-- Color added

The code is not so neat neigther sophisticated, but does the job.


And just to let you know, it is important to include the line

\pgfplotsset{compat=1.15}

in preamble when using PGFPlots. See this answer for further information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.