我找到了一个很好的答案这里,根据值解决矩阵图中文本变为白色或黑色的问题。
我希望将其应用于组图,但由于组图的两个部分处于不同的尺度,因此颜色变化的值需要有所不同。
我的问题有两个:a)我无法通过将其复制到我的groupplots中来使它工作,b)这段代码非常庞大,而我真正想做的只是在\nextgrouplot
当前point meta min
设置旁边看到一个类似的设置change colour = 0.3
。
那么,如何将其适应于群图,以便可以设置变化的级别,并且大多数代码只需输入一次。
姆韦用于制作具有不同比例的并排矩阵图。
% !TeX program = lualatex
\documentclass[border=5mm,tikz]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{colormaps}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.15}
\usepackage{fontspec}
\pgfplotsset{style matrix/.style= {matrix plot, nodes near coords, nodes near coords align=center, opacity=0.8,font=\bfseries\large}}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[group style={group size=2 by 1, horizontal sep=2cm,
xlabels at=edge bottom
},
colormap name=viridis,
typeset ticklabels with strut,
nodes near coords style={
/pgf/number format/fixed,
},
]
\nextgroupplot[
view={0}{90},
colorbar,
xtick=data,
ytick=data,
xticklabels={,,,},
yticklabels={,,,},
point meta min=0.4,
point meta max=0.7,
]
\addplot3[style matrix] table [meta=z]{
x y z
0 0 0.507006787825706
1 0 0.419818693341669
2 0 0.41
3 0 0.513713862120089
0 1 0.474709875191592
1 1 0.604668125455872
2 1 0.581785714285714
3 1 0.618143068939956
0 2 0.434311364134005
1 2 0.580077107429405
2 2 0.539107142857143
3 2 0.514269829503336
0 3 0.471863367637399
1 3 0.562259039283109
2 3 0.564821428571429
3 3 0.577372127501853
};
\nextgroupplot[
view={0}{90},
colorbar,
xtick=data,
ytick=data,
yticklabels={,,,},
xticklabels={,,,},
yticklabel style={rotate=90},
point meta min=0.0,
point meta max=1.0,
]
\addplot3[style matrix] table[meta=z] {
x y z
0 0 0.883183709218305
1 0 0.695008856934459
2 0 0.797767857142857
3 0 0.784006671608599
0 1 0.718086271075104
1 1 0.80150046889653
2 1 0.745178571428571
3 1 0.743606375092661
0 2 0.955386468141012
1 2 0.890486610399083
2 2 0.876339285714286
3 2 0.842105263157895
0 3 0.718086271075104
1 3 0.701573408356778
2 3 0.745178571428571
3 3 0.827001482579689
};
\end{groupplot}
\end{tikzpicture}
\end{document}
答案1
这定义了一个 style change color
,它接受一个参数,即某种临界值,低于该值时文本将变为白色。代码中的关键部分是
\pgfplotsset{change nnc color/.code={%
\pgfmathtruncatemacroFPU{\mysign}{ifthenelse(\pgfplotspointmeta-#1>0,1,0)}%
\ifcase\mysign
\pgfkeysalso{/tikz/text=white}
\or
\pgfkeysalso{/tikz/text=black}
\fi
}}
检查和参数之间的差值的符号\pgfplotspointmeta
,即临界值。从概念上讲,这段代码非常类似于您链接到的答案但也许更容易理解一些(?)并且没有定义全局宏(在写答案时,\pgfmathsmuggle
尚未公开)。
\documentclass[border=5mm,tikz]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{colormaps}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.15}
\usepackage{fontspec}
\pgfplotsset{style matrix/.style= {matrix plot, nodes near coords, nodes near coords align=center, opacity=0.8,font=\bfseries\large}}
\newcommand{\pgfmathtruncatemacroFPU}[2]{\begingroup%
\pgfkeys{/pgf/fpu}%
\pgfmathtruncatemacro{#1}{#2}%
\pgfmathsmuggle#1\endgroup}
\pgfplotsset{change nnc color/.code={%
\pgfmathtruncatemacroFPU{\mysign}{ifthenelse(\pgfplotspointmeta-#1>0,1,0)}%
\ifcase\mysign
\pgfkeysalso{/tikz/text=white}
\or
\pgfkeysalso{/tikz/text=black}
\fi
},
change color/.style={every node near coord/.append style={%
/pgfplots/change nnc color=#1
}},
change color/.default=0.5}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[group style={group size=2 by 1, horizontal sep=2cm,
xlabels at=edge bottom
},
colormap name=viridis,
typeset ticklabels with strut,
nodes near coords style={
/pgf/number format/fixed,
},
]
\nextgroupplot[
view={0}{90},
colorbar,
xtick=data,
ytick=data,
xticklabels={,,,},
yticklabels={,,,},
point meta min=0.4,
point meta max=0.7,
change color
]
\addplot3[style matrix] table [meta=z]{
x y z
0 0 0.507006787825706
1 0 0.419818693341669
2 0 0.41
3 0 0.513713862120089
0 1 0.474709875191592
1 1 0.604668125455872
2 1 0.581785714285714
3 1 0.618143068939956
0 2 0.434311364134005
1 2 0.580077107429405
2 2 0.539107142857143
3 2 0.514269829503336
0 3 0.471863367637399
1 3 0.562259039283109
2 3 0.564821428571429
3 3 0.577372127501853
};
\nextgroupplot[
view={0}{90},
colorbar,
xtick=data,
ytick=data,
yticklabels={,,,},
xticklabels={,,,},
yticklabel style={rotate=90},
point meta min=0.0,
point meta max=1.0,
change color=0.75
]
\addplot3[style matrix] table[meta=z] {
x y z
0 0 0.883183709218305
1 0 0.695008856934459
2 0 0.797767857142857
3 0 0.784006671608599
0 1 0.718086271075104
1 1 0.80150046889653
2 1 0.745178571428571
3 1 0.743606375092661
0 2 0.955386468141012
1 2 0.890486610399083
2 2 0.876339285714286
3 2 0.842105263157895
0 3 0.718086271075104
1 3 0.701573408356778
2 3 0.745178571428571
3 3 0.827001482579689
};
\end{groupplot}
\end{tikzpicture}
\end{document}