4

I have the following minimal example:

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.12}
\pgfplotsset{
  every axis/.style = {
    colormap name = viridis,
  },
}
\tikzset{
  cmapfill/.style = {
    color of colormap = {#1},
    draw = .!50!black,
    % text = .!50!black,
    fill = .!25!white,
  },
}
\begin{document}
\begin{tikzpicture}
\node[cmapfill = 200] (x) at (0, 0) {$x$};
\node[cmapfill = 700] (y) at (1, 0) {$y$};
\end{tikzpicture}
\end{document}

This works nicely, except that the text in the nodes has the color of the colormap, and I would like it to have the darker color .!50!black just like the border.

Text color is too light

If I uncomment the text = .!50!black line then the text indeed has the desired color, however the string !50!black appears in the labels too.

Text appears in label

How can I avoid changing the label text, but still change the text color?

Stefan Pinnow
  • 29,535
Ruud
  • 1,423

2 Answers2

5

Move the color . into temp and then use temp in the mixes:

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat = 1.12}
\pgfplotsset{
  every axis/.style = {
    colormap name = viridis,
  },
}

\tikzset{
  cmapfill/.style = {
    color of colormap = {#1},
    /utils/exec={\colorlet{temp}{.}},
    draw = temp!50!black,
    text = temp!50!black,
    fill = temp!25!white,
  },
}
\begin{document}
\begin{tikzpicture}
\node[cmapfill = 200] (x) at (0, 0) {$x$};
\node[cmapfill = 700] (y) at (1, 0) {$y$};
\end{tikzpicture}
\end{document} 
Hood Chatham
  • 5,467
0

Not an answer to the question, but as a workaround, I generated a list of pre-defined colors using Matplotlib, which is where pgfplots got the colormap from in the first place:

from matplotlib import cm
cmap = cm.get_cmap('viridis')
for z in range(0, 51):
  print('\\definecolor{{viridis{}}}{{rgb}}{{{},{},{}}}'.format(z, *cmap(z * 0.02)))

This prints

\definecolor{viridis0}{rgb}{0.267004,0.004874,0.329415}
...
\definecolor{viridis50}{rgb}{0.993248,0.906157,0.143936}

After defining

\tikzset{
  viridis/.style = {
    text = viridis#1!75!black,
    draw = viridis#1!75!black,
    fill = viridis#1!25!white,
  },
}

It can then be used as

\begin{tikzpicture}
\node[viridis = 10] (x) at (0, 0) {$x$};
\node[viridis = 35] (y) at (1, 0) {$y$};
\end{tikzpicture}

This works in TikZ without loading pgfplots at all.

Ruud
  • 1,423