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?

  • . is not understood by TikZ but it uses it internally because that's what xcolor understands. It's not meant for end user. – percusse Feb 15 '18 at 11:59
  • @PhelypeOleinik, #1 is not a color, it is the progress of the colormap. If I replace . with #1 I get Package xcolor Error: Undefined color '200'. – Ruud Feb 15 '18 at 12:42
  • @percusse, the . was suggested in this answer. If it is not intended for users, what should I use instead? – Ruud Feb 15 '18 at 12:43
  • @Ruud Oops, sorry. I'll delete my comment :) – Phelype Oleinik Feb 15 '18 at 12:44
  • @Ruud I said TikZ ;) not pgfplots. There is a different mechanism for text and path colors.` – percusse Feb 15 '18 at 12:47
4

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} 
  • nice answer +1). however all explicit loading of tikz is superfluous. loading pgfplots is sufficient. – Zarko Feb 15 '18 at 13:30
  • @Zarko I just copied that stuff from his MWE. Edited both. – Hood Chatham Feb 15 '18 at 13:33
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.