我有以下最小示例:
\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}
这很好用,只是节点中的文本具有颜色图的颜色,而我希望它.!50!black
像边框一样具有更深的颜色。
如果我取消注释该text = .!50!black
行,则文本确实具有所需的颜色,但字符串!50!black
也会出现在标签中。
如何避免更改标签文本,但仍更改文本颜色?
答案1
将颜色移入.
然后temp
在temp
混合中使用:
\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}
答案2
这不是问题的答案,但作为一种解决方法,我使用 Matplotlib 生成了一个预定义颜色列表,这是 pgfplots 首先从中获取颜色图的地方:
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)))
这将打印
\definecolor{viridis0}{rgb}{0.267004,0.004874,0.329415}
...
\definecolor{viridis50}{rgb}{0.993248,0.906157,0.143936}
定义后
\tikzset{
viridis/.style = {
text = viridis#1!75!black,
draw = viridis#1!75!black,
fill = viridis#1!25!white,
},
}
然后它可以用作
\begin{tikzpicture}
\node[viridis = 10] (x) at (0, 0) {$x$};
\node[viridis = 35] (y) at (1, 0) {$y$};
\end{tikzpicture}
这在 TikZ 中有效,根本不需要加载 pgfplots。