我正在尝试格式化矩阵图,使节点根据单元格颜色改变文本颜色。这比我想象的要复杂得多。
以下代码在\ifthenelse
命令处以某种方式卡住了,我不知道如何让它正常运行。
% contents of matrix.txt
x y v
0 0 78
1 0 63
2 0 42
3 0 51
4 0 99
...
% MWE
\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{ifthen}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\begin{axis}[enlargelimits=false, axis lines=none, colormap/blackwhite]
\addplot [
matrix plot,
visualization depends on={value \ifthenelse{\thisrow{v} < 90}{white}{black} \as \tcolor},
nodes near coords,
scatter/@pre marker code/.append style={/tikz/text=\tcolor},
mesh/cols=8,
point meta=explicit,
] table [x=x, y=y, meta=v] {matrix.txt};
\end{axis}
\end{tikzpicture}
\end{document}
任何帮助将非常感激!
答案1
作为@egreg 指出,\ifthenelse
不是合适的工具。而且不需要加载,ifthen
因为 Ti钾Z 有一个内置函数ifthenelse
,您可以按如下方式使用它:
\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{filecontents*}{matrix.txt}
x y v
1 0 63
2 0 42
3 0 51
4 0 99
1 1 63
2 1 42
3 1 51
4 1 99
\end{filecontents*}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\begin{axis}[enlargelimits=false, axis lines=none, colormap/blackwhite]
\addplot [
matrix plot,
visualization depends on={%
ifthenelse(\thisrow{v}>90,100,0) \as \myv},
nodes near coords,
scatter/@pre marker code/.append style={/tikz/text=black!\myv},
mesh/cols=4,mesh/rows=2,
point meta=explicit,
] table [x=x, y=y, meta=v] {matrix.txt};
\end{axis}
\end{tikzpicture}
\end{document}