数据可视化图例的样式

数据可视化图例的样式

我在数据可视化环境中有一堆图,每个图的样式都不同。当我将它们收集到图例中时,它们都显示为黑色。我根据手册中的示例构建了一个快速 MWE

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{datavisualization}

\begin{document}
\begin{tikzpicture}
  \datavisualization data group {lines} = {
   data point [x=0, y=0, set=normal]
data point [x=2, y=2, set=normal]
data point [x=0, y=1, set=heated]
data point [x=2, y=1, set=heated]
data point [x=0.5, y=1.5, set=critical]
data point [x=2.25, y=1.75, set=critical]
                                };
  \datavisualization [
school book axes={unit=0.3},
visualize as line=normal,
visualize as line=heated,
visualize as line=critical,
normal={style={green}, label in legend={text={normal}}},
heated={style={yellow}, label in legend={text={heated}}},
critical={style={red}, label in legend={text={critical}}},
legend=north east inside]
  data group {lines};
\end{tikzpicture}
\end{document}

我是否需要在某处再次指定颜色信息?使用样式表时,颜色会正确显示在图例中,但我更希望单独设置颜色。

在此处输入图片描述

答案1

作为一种解决方法,您可以创建一个使用与线条相同颜色的样式表。然后xelatex创建一个彩色图例。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{datavisualization}

\pgfdvdeclarestylesheet{Huang d}{
  1/.style={green!50!black},
  2/.style={yellow!90!black},
  3/.style={red!80!black},
  default style/.style={black}
}

\begin{document}
\begin{tikzpicture}
  \datavisualization data group {lines} = {
   data point [x=0, y=0, set=normal]
data point [x=2, y=2, set=normal]
data point [x=0, y=1, set=heated]
data point [x=2, y=1, set=heated]
data point [x=0.5, y=1.5, set=critical]
data point [x=2.25, y=1.75, set=critical]
                                };
  \datavisualization [
school book axes={unit=0.3},style sheet=Huang d,
visualize as line=normal,
visualize as line=heated,
visualize as line=critical,
normal={style={green}, label in legend={text={normal}}},
heated={style={yellow}, label in legend={text={heated}}},
critical={style={red}, label in legend={text={critical}}},
legend=north east inside]
  data group {lines};
\end{tikzpicture}
\end{document}

在此处输入图片描述

更简单的可能性是Huang_d 的回答,可以进一步简化为

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{datavisualization}

\pgfdvdeclarestylesheet{xelatex workaround}{
  default style/.style={black}
}

\begin{document}
\begin{tikzpicture}
  \datavisualization data group {lines} = {
   data point [x=0, y=0, set=normal]
data point [x=2, y=2, set=normal]
data point [x=0, y=1, set=heated]
data point [x=2, y=1, set=heated]
data point [x=0.5, y=1.5, set=critical]
data point [x=2.25, y=1.75, set=critical]
                                };
  \datavisualization [
school book axes={unit=0.3},style sheet=xelatex workaround,
visualize as line=normal,
visualize as line=heated,
visualize as line=critical,
normal={style={green}, label in legend={text={normal}}},
heated={style={yellow}, label in legend={text={heated}}},
critical={style={red}, label in legend={text={critical}}},
legend=north east inside]
  data group {lines};
\end{tikzpicture}
\end{document}

这给出了一个通用输出,即,对于pdflatexxelatex和 来说相同lualatex

在此处输入图片描述

答案2

问题似乎是 Xe(La)TeX 与 Ti 之间的相互作用Z. 为了获得带有正确颜色标签的黑色文本,需要使用黑色样式表。然后可以按预期设置颜色:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{datavisualization}

\pgfdvdeclarestylesheet{allblack}{default style/.style={black}}

\begin{document}
\begin{tikzpicture}
  \datavisualization data group {lines} = {
   data point [x=0, y=0, set=normal]
data point [x=2, y=2, set=normal]
data point [x=0, y=1, set=heated]
data point [x=2, y=1, set=heated]
data point [x=0.5, y=1.5, set=critical]
data point [x=2.25, y=1.75, set=critical]
                                };
  \datavisualization [
school book axes={unit=0.3},style sheet=allblack,
visualize as line=normal,
visualize as line=heated,
visualize as line=critical,
normal={style={green}, label in legend={text={normal}}},
heated={style={yellow}, label in legend={text={heated}}},
critical={style={red}, label in legend={text={critical}}},
legend=north east inside]
  data group {lines};
\end{tikzpicture}
\end{document}

这样就得到了正确颜色的线条和黑色文本。如果需要,可以使用样式表更改文本颜色。

相关内容