4

The following MWE is taken from here.

I have a correlation matrix, and for simplicity, I would like to leave the upper triangle empty. However, the code breaks when I do that (probably because of the if phrase).

How do I need to adjust the code to allow for empty cells?

\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}%
                \pgfmathtruncatemacro\number{##1}%
                \ifnum\number<0
                    \cellcolor{red!-##1}##1
              \else 
                  \cellcolor{green!##1}##1
              \fi
              }},
        columns/x/.style={
            column name={},
            postproc cell content/.code={}
        }
    }
}

\begin{document}
\begin{table}\caption{Correlation or something}
\centering
\pgfplotstabletypeset[color cells]{
x,a,b,c,d
a,90,-10.5,0,0
b,0,80,10,-10
c,0,0,-95,5
d,0,10,5,-85
}
\end{table}
\end{document}

enter image description here

Xiphias
  • 1,233

1 Answers1

5

You could additionally test if the cell content is empty

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.15}
\usepackage{etoolbox}

\pgfplotstableset{
    color cells/.style={
       col sep=comma,
       string type,
       postproc cell content/.code={%
             \pgfkeysalso{@cell content=\rule{0cm}{2.4ex}%
                  \ifstrequal{##1}{}{}{%
                      \pgfmathtruncatemacro\number{##1}%
                      \ifnum\number<0
                          \cellcolor{red!-##1}##1
                      \else 
                            \cellcolor{green!##1}##1
                      \fi}%
             }},
        columns/x/.style={
            column name={},
            postproc cell content/.code={}
        }
    }
}

\begin{document}
\begin{table}\caption{Correlation or something}
\centering
\pgfplotstabletypeset[color cells]{
x,a,b,c,d
a,90,,,
b,0,80,,
c,0,0,-95,
d,0,10,5,-85
}
\end{table}
\end{document}

Result:

enter image description here

Or you could test if the cell content is a number:

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.15}
\usepackage{scrbase}

\pgfplotstableset{
    color cells/.style={
       col sep=comma,
       string type,
       postproc cell content/.code={%
             \pgfkeysalso{@cell content=\rule{0cm}{2.4ex}%
                  \ifisdimension{##1 pt}{%
                      \ifdim ##1 pt<0pt
                          \cellcolor{red!-##1}##1
                      \else 
                          \cellcolor{green!##1}##1
                      \fi}{}%
             }},
        columns/x/.style={
            column name={},
            postproc cell content/.code={}
        }
    }
}

\begin{document}
\begin{table}\caption{Correlation or something}
\centering
\pgfplotstabletypeset[color cells]{
x,a,b,c,d
a,90,,,
b,0,80,,
c,0,0,-95,
d,0,10,5,-85
}
\end{table}
\end{document}

The result is the same as above.

esdd
  • 85,675