1

I found a very nice answer here, that solves turning text white or black in a matrix plot depending on the value.

I wish to apply this to a groupplot, but the value at which the color should change needs to be different for the two parts of the groupplot since they are on a different scale.

My problem is twofold: a) I couldn't for the life of me get this working by copying it into my groupplots, and b) the piece of code is HUGE, while I would really only like to see a setting within the \nextgrouplot alongside the current point meta min setting with something like change colour = 0.3.

So how would you adapt this to a groupplot so the level of change can be set and most of the code is only typed once.

mwe for making a side-by-side matrix plot with different scales.

% !TeX program = lualatex
\documentclass[border=5mm,tikz]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{colormaps}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.15}
\usepackage{fontspec}

\pgfplotsset{style matrix/.style= {matrix plot, nodes near coords, nodes near coords align=center, opacity=0.8,font=\bfseries\large}}

\begin{document}

\begin{tikzpicture}
\begin{groupplot}[group style={group size=2 by 1, horizontal sep=2cm,
xlabels at=edge bottom
},
colormap name=viridis,
typeset ticklabels with strut,
nodes near coords style={
  /pgf/number format/fixed,
},
]
  \nextgroupplot[
     view={0}{90},
     colorbar,
     xtick=data,
     ytick=data,
     xticklabels={,,,},
     yticklabels={,,,},
     point meta min=0.4,
     point meta max=0.7,
]
\addplot3[style matrix] table [meta=z]{
x   y   z
0   0   0.507006787825706
1   0   0.419818693341669
2   0   0.41
3   0   0.513713862120089

0   1   0.474709875191592
1   1   0.604668125455872
2   1   0.581785714285714
3   1   0.618143068939956

0   2   0.434311364134005
1   2   0.580077107429405
2   2   0.539107142857143
3   2   0.514269829503336

0   3   0.471863367637399
1   3   0.562259039283109
2   3   0.564821428571429
3   3   0.577372127501853
};


    \nextgroupplot[
       view={0}{90},
       colorbar,
       xtick=data,
       ytick=data,
       yticklabels={,,,},
       xticklabels={,,,},
       yticklabel style={rotate=90},
       point meta min=0.0,
       point meta max=1.0,
  ]
  \addplot3[style matrix] table[meta=z] {
x   y   z
0   0   0.883183709218305
1   0   0.695008856934459
2   0   0.797767857142857
3   0   0.784006671608599

0   1   0.718086271075104
1   1   0.80150046889653
2   1   0.745178571428571
3   1   0.743606375092661

0   2   0.955386468141012
1   2   0.890486610399083
2   2   0.876339285714286
3   2   0.842105263157895

0   3   0.718086271075104
1   3   0.701573408356778
2   3   0.745178571428571
3   3   0.827001482579689
};
\end{groupplot}
\end{tikzpicture}
\end{document}
Stefan Pinnow
  • 29,535
fbence
  • 1,585
  • 1
    Would it be possible to split up the question? While I feel rather confident to be able to provide you with the change color key in the way you describe, I have no clue whatsoever with regards to the \input part; I do not even understand the question. –  Feb 11 '20 at 23:54
  • actually, you are right, it should be two questions. – fbence Feb 12 '20 at 00:04
  • I narrowed it down to just making a change color key and make a separate question about outsourcing. – fbence Feb 12 '20 at 00:05
  • @Schrödinger'scat I put the other part of my question here: https://tex.stackexchange.com/q/528074/51093 if you are interested. – fbence Feb 12 '20 at 08:56

1 Answers1

1

This defines a style change color, which takes an argument, some sort of critical value below which the text turns white. The essential piece in the code is

\pgfplotsset{change nnc color/.code={%
\pgfmathtruncatemacroFPU{\mysign}{ifthenelse(\pgfplotspointmeta-#1>0,1,0)}%
\ifcase\mysign
\pgfkeysalso{/tikz/text=white}
\or
\pgfkeysalso{/tikz/text=black}
\fi
}}

which checks the sign of the difference between \pgfplotspointmeta and the argument, i.e. the critical value. Conceptually this code is very much like the answer you link to but perhaps a bit easier to understand (?) and does not define a global macro (at the time the answer was written, \pgfmathsmuggle was not yet public).

\documentclass[border=5mm,tikz]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{colormaps}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.15}
\usepackage{fontspec}

\pgfplotsset{style matrix/.style= {matrix plot, nodes near coords, nodes near coords align=center, opacity=0.8,font=\bfseries\large}}
\newcommand{\pgfmathtruncatemacroFPU}[2]{\begingroup%
\pgfkeys{/pgf/fpu}%
\pgfmathtruncatemacro{#1}{#2}%
\pgfmathsmuggle#1\endgroup}

\pgfplotsset{change nnc color/.code={%
\pgfmathtruncatemacroFPU{\mysign}{ifthenelse(\pgfplotspointmeta-#1>0,1,0)}%
\ifcase\mysign
\pgfkeysalso{/tikz/text=white}
\or
\pgfkeysalso{/tikz/text=black}
\fi
},
change color/.style={every node near coord/.append style={%
/pgfplots/change nnc color=#1
}},
change color/.default=0.5}
\begin{document}

\begin{tikzpicture}
\begin{groupplot}[group style={group size=2 by 1, horizontal sep=2cm,
xlabels at=edge bottom
},
colormap name=viridis,
typeset ticklabels with strut,
nodes near coords style={
  /pgf/number format/fixed,
},
]
  \nextgroupplot[
     view={0}{90},
     colorbar,
     xtick=data,
     ytick=data,
     xticklabels={,,,},
     yticklabels={,,,},
     point meta min=0.4,
     point meta max=0.7,
     change color
]
\addplot3[style matrix] table [meta=z]{
x   y   z
0   0   0.507006787825706
1   0   0.419818693341669
2   0   0.41
3   0   0.513713862120089

0   1   0.474709875191592
1   1   0.604668125455872
2   1   0.581785714285714
3   1   0.618143068939956

0   2   0.434311364134005
1   2   0.580077107429405
2   2   0.539107142857143
3   2   0.514269829503336

0   3   0.471863367637399
1   3   0.562259039283109
2   3   0.564821428571429
3   3   0.577372127501853
};


    \nextgroupplot[
       view={0}{90},
       colorbar,
       xtick=data,
       ytick=data,
       yticklabels={,,,},
       xticklabels={,,,},
       yticklabel style={rotate=90},
       point meta min=0.0,
       point meta max=1.0,
       change color=0.75
  ]
  \addplot3[style matrix] table[meta=z] {
x   y   z
0   0   0.883183709218305
1   0   0.695008856934459
2   0   0.797767857142857
3   0   0.784006671608599

0   1   0.718086271075104
1   1   0.80150046889653
2   1   0.745178571428571
3   1   0.743606375092661

0   2   0.955386468141012
1   2   0.890486610399083
2   2   0.876339285714286
3   2   0.842105263157895

0   3   0.718086271075104
1   3   0.701573408356778
2   3   0.745178571428571
3   3   0.827001482579689
};
\end{groupplot}
\end{tikzpicture}
\end{document}

enter image description here

  • If I could I'd give another upvote for you profile pic :D – fbence Feb 12 '20 at 08:45
  • I am trying to understand what is happening here (since the general structure seems pretty useful :D). I'm guessing the newcommand part does some magic to allow floating point calculations with pgfmathtruncatemacro. I couldn't find pgfmathsmuggle in the documentation though ... I also absolutely don't get the syntax for the ifthenelse part and also can't find it in the documentation. Where can I read about these? It be good to know math in pgfplots ... – fbence Feb 12 '20 at 09:02
  • 1
    @fbence All of them can be found in the pgfmanual v3.1.5. \pgfmathsmuggle is documented on p. 1046, and ifthenelse in section 95.3 Syntax for Mathematical Expressions: Functions. –  Feb 12 '20 at 14:59
  • I was looking in the pgfplots manual ... – fbence Feb 13 '20 at 15:09