3

I'm trying to make a heatmap and make 0.0 values "blank", like just a blank box, I have no clue how to do that. I have tried putting NaNs and using witchery but hasn't worked. I use the same kind of heatmap that was given on another tex.stackexchange.

% Posted as an answer to https://tex.stackexchange.com/q/44868.

\documentclass[tikz]{standalone}

\begin{document}
\begin{tikzpicture}[scale=0.6]
  \foreach \y [count=\n] in {
      {74,25,39,20,3,3,3,3,3},
      {25,53,31,17,7,7,2,3,2},
      {39,31,37,24,3,3,3,3,3},
      {20,17,24,37,2,2,6,5,5},
      {3,7,3,2,12,1,0,0,0},
      {3,7,3,2,1,36,0,0,0},
      {3,2,3,6,0,0,45,1,1},
      {3,3,3,5,0,0,1,23,1},
      {3,2,3,5,0,0,1,1,78},
    } {
      % column labels
      \ifnum\n<10
        \node[minimum size=6mm] at (\n, 0) {\n};
      \fi
      % heatmap tiles
      \foreach \x [count=\m] in \y {
        \node[fill=yellow!\x!purple, minimum size=6mm, text=white] at (\m,-\n) {\x};
      }
    }

  % row labels
  \foreach \a [count=\i] in {a,b,c,d,e,f,g,h,i} {
    \node[minimum size=6mm] at (0,-\i) {\a};
  }
\end{tikzpicture}
\end{document}

Heatmap from the code

1 Answer 1

4

enter image description here

\documentclass[tikz]{standalone}

\begin{document}
\begin{tikzpicture}[scale=0.6]
  \foreach \y [count=\n] in {
      {74,25,39,20,3,3,3,3,3},
      {25,53,31,17,7,7,2,3,2},
      {39,31,37,24,3,3,3,3,3},
      {20,17,24,37,2,2,6,5,5},
      {3,7,3,2,12,1,0,0,0},
      {3,7,3,2,1,36,0,0,0},
      {3,2,3,6,0,0,45,1,1},
      {3,3,3,5,0,0,1,23,1},
      {3,2,3,5,0,0,1,1,78},
    } {
      % column labels
      \ifnum\n<10
        \node[minimum size=6mm] at (\n, 0) {\n};
      \fi
      % heatmap tiles
      \foreach \x [count=\m] in \y {
        \node[fill=yellow!\x!purple, minimum size=6mm, text=white] at (\m,-\n) {\pgfmathparse{ifthenelse(\x==0,"","\x")}\pgfmathresult};
      }
    }

  % row labels
  \foreach \a [count=\i] in {a,b,c,d,e,f,g,h,i} {
    \node[minimum size=6mm] at (0,-\i) {\a};
  }
\end{tikzpicture}
\end{document}
4
  • Even quicker (hackier?) hack: \node[fill=yellow!\x!purple, minimum size=6mm, text=white] at (\m,-\n) {\ifnum\x=0\else\x\fi};
    – Marijn
    Commented Aug 7 at 15:24
  • @Marijn You should post an answer! Commented Aug 7 at 15:25
  • I was in the middle of it :) but I thought it was too similar to yours so I just left the comment. Actually yours is more flexible, for example if you want to give a special color like white or grey for the zero nodes.
    – Marijn
    Commented Aug 7 at 15:26
  • @Marijn I moved to a pgf based solution. You can write an ifnum answer if you like. Commented Aug 7 at 15:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .