多个水平堆叠条形标签重叠

多个水平堆叠条形标签重叠

我正在尝试使用以下代码在一个图中绘制多个堆栈条形图。但每个堆栈的值并未放置在每个条形内。请帮忙。

\begin{tikzpicture}
\begin{axis}[
    xbar stacked,
    width=15.6cm, height=6.0cm, 
    bar width=15pt,
    %nodes near coords,
    nodes near coords,
    %enlargelimits=0.15,
    %enlarge y limits=0.01,
    %enlargelimits=0.07,
    legend style={at={(0.5,-0.20)}, anchor=north,legend columns=-1},
    %ylabel={\#participants},
    symbolic y coords={A, B, C, D, E},
    ytick=data,
    ]
    \addplot+[xbar] plot coordinates {(54,A) (49,B) (42,C) (41,D) (37,E)};
    \addplot+[xbar] plot coordinates {(3,A) (7,B) (1,C) (7,D) (16,E)};
    \addplot+[xbar] plot coordinates {(34,A) (28,B) (50,C) (43,D) (30,E)};
    \addplot+[xbar] plot coordinates {(12,A) (20,B) (9,C) (11,D) (19,E)};
    \addplot+[xbar] plot coordinates {(3,A) (2,B) (4,C) (4,D) (4,E)};
    \legend{\strut Agree, \strut Strongly agree, \strut neutral, \strut Disagree, \strut Strongly disagree}
\end{axis}
\end{tikzpicture}

答案1

显著的变化:

  • 替换\addplot+[xbar] plot coordinates {...}\addplot coordinates {...};
  • 明确指定xminxmax
  • 改变第五个情节系列的颜色。

您可以在以下位置找到有关堆叠图的更多示例PGFplots 手册,第 4.5.9 节。

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
      xbar stacked,
      width=15.6cm, height=6.0cm, 
      bar width=15pt,
      nodes near coords,
      xmin=0, xmax=106,
      %enlargelimits=0.15,
      %enlarge y limits=0.01,
      %enlargelimits=0.07,
      legend style={at={(0.5,-0.20)}, anchor=north, legend columns=-1},
      %ylabel={\#participants},
      symbolic y coords={A, B, C, D, E},
      ytick=data,
    ]
      \addplot coordinates {(54,A) (49,B) (42,C) (41,D) (37,E)};
      \addplot coordinates {(3,A) (7,B) (1,C) (7,D) (16,E)};
      \addplot coordinates {(34,A) (28,B) (50,C) (43,D) (30,E)};
      \addplot coordinates {(12,A) (20,B) (9,C) (11,D) (19,E)};
      \addplot[color=violet, fill=violet!50] coordinates {(3,A) (2,B) (4,C) (4,D) (4,E)};
      \legend{\strut Agree, \strut Strongly agree, \strut neutral, \strut Disagree, \strut Strongly disagree}
  \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容