0

I just created this heatmap, following this template: https://tikz.net/heatmap/

Now I would like to customize a few things:

  1. If I replace the Column Label with $j=1$ bsp. then the labels overlap. How can I make the boxes in the square larger so that the text fits well on it?
  2. Unfortunately, because of the smaller colors, the heat gradations are different. How do I adjust this to my values, so that three is the maximum?

Here is my code:

\documentclass[tikz]{standalone}

\begin{document} \begin{tikzpicture}[scale=0.6] \foreach \y [count=\n] in { {0,1,2,0,0,1,2,1,3,2,1,2,1,1}, {0,1,0,0,1,1,1,1,0,0,0,0,1,0}, {3,1,1,3,2,1,0,1,0,1,2,1,1,2}, } {

  % heatmap tiles
  \foreach \x [count=\m] in \y {
    \node[fill=purple!\x!yellow, minimum size=6mm, text=white] at (\m,-\n) {\x};
  }
}

% row labels \foreach \a [count=\i] in {$k=1~~~$,$k=2~$,$k=3~~~$} { \node[minimum size=6mm] at (0,-\i) {\a}; } \foreach \b [count=\j] in {1,2,3,4,5,6,7,8,9,10,11,12,13,14} { \node[minimum size=6mm] at (\j,0) {\b}; } \end{tikzpicture} \end{document}

  • Welcome. The number between two colors (i.e. where \x goes) from 0 to 100, so you will need to multiply \x by 100/3 to get the whole spectrum from purple to yellow. The row labels are better set with left instead of a bunch of ~s. For the column headers you will need to make a decision. Should the heatmap tiles just scretch in the horizontal direction or should the remain square? You will have to change minimum width or minimum size respectively. – Qrrbrbirlbel Jul 21 '23 at 13:18
  • Thanks. Which \x do i need to multiply? Would \x*(100/3)work? – marvelfab12 Jul 21 '23 at 13:26
  • Would you be willing to provide the code for the whole % heatmap tiles section? I cant get it to work. Thanks a lot – marvelfab12 Jul 21 '23 at 13:34
  • Related question and also the source of your original code as well as other approaches: Q44868 – Qrrbrbirlbel Jul 21 '23 at 13:42

1 Answers1

1

Short of using a matrix or other matrix-creating packages/tools I'd approach this with chains to not have to think about any coordinates at all.

With setting a common text width for almost all nodes we can achieve a uniform output.
Though, now the diagram is 18cm wide.

The values for text height and text depth are set globally for all nodes for the same effect in the vertical dimension. (The j in the headings needs to be taken care of, otherwise they are almost the same size anyway.)

I've added two rectangular paths, one encompasses the whole diagram (though, the backgrounds library might be better suited since it also has its own interface to adjust the padding and style) and one just the heatmap boxes.

I've chosen different line styles to show their overlap better.

Code

\documentclass[tikz, border=2mm]{standalone}
\usetikzlibrary{chains}
\begin{document}
\begin{tikzpicture}[
  node distance=+0pt,
  text height=.8em, text depth=+.2em, outer sep=+0pt, align=center,
  box/.style ={text width={width("$j = 00$")}},
  box1/.style={text width={width("$k = 0$")}, align=right},
  start chain=rows going below,
]
\node[on chain, box1]{};
\scoped[start branch=k0 going base right]
  \foreach \b in {1, ..., 14} \node[on chain, box] {$j = \b$};

\foreach \y [count=\n] in { {0,1,2,0,0,1,2,1,3,2,1,2,1,1}, {0,1,0,0,1,1,1,1,0,0,0,0,1,0}, {3,1,1,3,2,1,0,1,0,1,2,1,1,2}}{ \node[on chain] {$k = \n$}; \tikzset{start branch/.expanded=k\n\space going base right} \foreach \x [evaluate={ \xc = \x*100/3; \tc = \xc < 33 ? "black" : "white";}] in \y \node[fill=purple!\xc!yellow, box, text=\tc, on chain] {\x}; }

\draw[green, ultra thick] (current bounding box.south west) rectangle (current bounding box.north east); \draw[blue] (rows/k3-2.south west) rectangle (rows/k1-end.north east); \end{tikzpicture} \end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821