如何使文本标签出现在 pgfplots xbar 堆叠中

如何使文本标签出现在 pgfplots xbar 堆叠中

我正在尝试让堆积条形图像饼图一样工作,每个矩形中都显示百分比和标签。我可以让值显示百分比符号,但不显示实际文本。也许还有其他东西需要作为 的参数,或者完全以nodes near coords其他方式。这是我的 mwe:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\begin{document}
\begin{tikzpicture}
\begin{axis}[width=1.19\linewidth, axis lines=none, xbar stacked, nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%}]
\addplot+[black,fill=white,xbar] plot coordinates {(10,0) [Homework]};
\addplot+[black,fill=white,xbar] plot coordinates {(10,0) [Quizzes]};
\addplot+[black,fill=white,xbar] plot coordinates {(20,0) [Midterm 1]};
\addplot+[black,fill=white,xbar] plot coordinates {(20,0) [Midterm 2]};
\addplot+[black,fill=white,xbar] plot coordinates {(40,0) [Final]};
\end{axis}
\end{tikzpicture}
\end{document}

我希望文本标签(“家庭作业”,“测验”等)出现在每个矩形内或下方,也许旋转某个角度以便它们适合可用的宽度。

答案1

默认情况\pgpflotspointmetaxbar,正如您所见,是 x 值。要访问您添加的文本,您需要设置point meta=explicit symbolic,这意味着pgfplots将使用您在坐标列表中明确写入的任何内容作为元值,并且它是不是解释为数字(这是symbolic部分)。

另外,要访问 x 值,您可以使用visualization depends on,如本例所示:

以下代码的输出

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
  % needed to increase width a bit
  width=1.4\linewidth,
  axis lines=none,
  xbar stacked,
  % increased bar width to make room for text
  bar width=30pt,
  point meta=explicit symbolic,
  visualization depends on=x \as \XVal,
  nodes near coords={%
    \pgfmathprintnumber\XVal\% \\
    \tiny\pgfplotspointmeta
  },
  every node near coord/.style={
      % allows for linebreaks
      align=center
  } 
]
\addplot+[black,fill=white] coordinates {(10,0) [Homework]};
\addplot+[black,fill=white] coordinates {(10,0) [Quizzes]};
\addplot+[black,fill=white] coordinates {(20,0) [Midterm 1]};
\addplot+[black,fill=white] coordinates {(20,0) [Midterm 2]};
\addplot+[black,fill=white] coordinates {(40,0) [Final]};
\end{axis}
\end{tikzpicture}
\end{document}

但这可能并不理想,而且也不太灵活(我找不到将元值存储在宏中的方法,如下所示)。在下面的例子中,我改变了坐标的输入方式,只是因为这确实使事情变得更容易、更灵活

plot注意:使用 时不需要关键字\addplot。也就是说,使用\addplot +[..] coordinates,而不是。并且,当您在选项中有 时\addplot +[..] plot coordinates,您不需要xbar为每个图指定。xbar stackedaxis

代码输出

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
  width=1.19\linewidth,
  axis lines=none, 
  xbar stacked,
  % save number from first table column in macro \XVal
  visualization depends on=\thisrowno{0} \as \XVal,
  % save number from third table column in macro \MetaVal
  % "value" means that it is _not_ parsed as a number
  visualization depends on=value \thisrowno{2} \as \MetaVal,
  % use \XVal in nodes near coords
  nodes near coords={\pgfmathprintnumber{\XVal}\%},
  % add a label to print the meta info
  every node near coord/.style={
    label={[anchor=west,rotate=-45]below:\MetaVal}
  }
]

\addplot +[black,fill=white] table[header=false,col sep=comma] {
10, 0, Homework
};
\addplot +[black,fill=white] table[header=false,col sep=comma] {
10, 0, Quizzes
};
\addplot +[black,fill=white] table[header=false,col sep=comma] {
20, 0, Midterm 1
};
\addplot +[black,fill=white] table[header=false,col sep=comma] {
20, 0, Midterm 2
};
\addplot +[black,fill=white] table[header=false,col sep=comma] {
40, 0, Final
};

\end{axis}
\end{tikzpicture}
\end{document}

相关内容