6

I have seen the following code in response to another similar question that works when the numbers in the table are between 0 and 100. How do I have to modify this code so that it works for numbers between 0 and 1 (or any arbitrary range?). My idea is that I need to modify the line:

\cellcolor{black!##1}

so that it somehow multiples this by 100, but I can't figure out how to do this.

Here is the full code that works for the numbers given (between 0 and 100) - I would like to do the same for number between 0 and 1. Thanks

\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}\cellcolor{black!##1}\pgfmathtruncatemacro\number{##1}\ifnum\number>50\color{white}\fi##1}%
                },
        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}
Werner
  • 603,163
Leo
  • 61
  • 3
  • Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. –  Jun 12 '14 at 20:08
  • 1
    Which one is the question you took this from? – percusse Jun 12 '14 at 20:13
  • It's this one "Parametrize shading in table through TikZ". In any case I have copied the full code below: – Leo Jun 12 '14 at 20:24
  • Sorry I'm new to this. I have edited the question to include the full code. Thanks – Leo Jun 12 '14 at 20:26
  • That code seems to be the one from my answer to Parametrize shading in table through TikZ. There's a much better implementation by Christian Feuersänger at Drawing heatmaps using TikZ – Jake Jun 13 '14 at 08:58
  • Thanks I have seen that code too.The problem with Christian's implementation is that it returns an error if the first column is comprised of string values rather than numbers.e.g., \pgfplotstabletypeset[ color cells={min=0,max=1}, col sep=comma, /pgfplots/colormap={whiteblue}{rgb255(0cm)=(255,255,255); rgb255(1cm)=(0,0,188)}, ] { x,zs, pd, zs,0.605,0.954 pd,0.444,0.998 } Also, is it be possible to implement the inverse shading of the text when the background becomes too dark that is in your code with Christian's? – Leo Jun 13 '14 at 10:14

1 Answers1

2

I've modified Christian's code a bit (basically destroyed visually and converted to a style) to limit the effective range of the macros defined. Otherwise string type columns also pick up the cell content setting directives. This allows to selectively enable the cell coloring.

\documentclass[border=5mm]{standalone}
\usepackage{pgfplotstable,colortbl}
\pgfplotsset{compat=1.12}

\pgfplotstableset{
  /color cells/min/.initial=0,
  /color cells/max/.initial=1000,
  /color cells/textcolor/.initial=,
  color cells/.style={
    postproc cell content/.append code={%
          \pgfkeys{/color cells/.cd,#1}%
      \pgfkeysgetvalue{/pgfplots/table/@preprocessed cell content}\value%
      \ifx\value\empty%
      \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}%
        \edef\temp{\noexpand\pgfkeyssetvalue{/pgfplots/table/@cell content}{%
            \noexpand\cellcolor[rgb]{\pgfmathresult}%
            \noexpand\definecolor{mapped color}{rgb}{\pgfmathresult}%
            \ifx\textcolorvalue\empty\else\noexpand\color{\textcolorvalue}\fi%
            \the\toks0%
          }%
        }%
        \temp%
      \fi%
      }%
  }%
}%

\begin{document}
\pgfplotstabletypeset[col sep=comma,
/color cells/max=100,
/color cells/min=0,
/color cells/textcolor=white,
columns/a/.style={color cells},
columns/b/.style={color cells},
columns/c/.style={color cells},
columns/d/.style={color cells},
columns/x/.style={string type},
/pgfplots/colormap/blackwhite,
]{
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{document}

enter image description here

percusse
  • 157,807
  • I am using this code to color my cells according to a heatmap. Could someone give me a tip what I need to change in the above code if I additionally want to have the numbers printed in bold? – Sven Rüberg Apr 09 '17 at 10:04