1

I am trying to draw multiple stack bar chart in one figure with following code. but values of each stack is not placed inside the each bar. Please help.

\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 Answer 1

2

Notable changes:

  • replace \addplot+[xbar] plot coordinates {...} by \addplot coordinates {...};,
  • explicitly specify xmin and xmax,
  • alter the color for fifth plot series.

You can find more examples about stacked plots in PGFplots manual, sec. 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}

enter image description here

1
  • Thank you so much. This code work
    – Waseem
    Jun 22, 2020 at 0:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .