![来自颜色表的 TikZ 样式文本颜色将文本添加到节点(不能使用例如“text=.!50!black”)](https://linux22.com/image/370540/%E6%9D%A5%E8%87%AA%E9%A2%9C%E8%89%B2%E8%A1%A8%E7%9A%84%20TikZ%20%E6%A0%B7%E5%BC%8F%E6%96%87%E6%9C%AC%E9%A2%9C%E8%89%B2%E5%B0%86%E6%96%87%E6%9C%AC%E6%B7%BB%E5%8A%A0%E5%88%B0%E8%8A%82%E7%82%B9%EF%BC%88%E4%B8%8D%E8%83%BD%E4%BD%BF%E7%94%A8%E4%BE%8B%E5%A6%82%E2%80%9Ctext%3D.!50!black%E2%80%9D%EF%BC%89.png)
我有以下最小示例:
\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。