我有以下最小示例:
\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。