1

I try to create a stacked bar plot with given values with the pgfplots package, but it shows some errors. Can anyone correct the given errors?

enter image description here

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
%\pgfplotsset{width=7cm,compat=1.8}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ybar stacked,
    bar width=15pt,
    nodes near coords,
    enlargelimits=0.15,
    legend style={at={(0.5,-0.20)},
      anchor=north,legend columns=-1},
    ylabel={\#Schedule Length},
    symbolic x coords={P1, P2, P3},
    xtick=data,
    x tick label style={rotate=45,anchor=east},
    xlabel={\#Processor}
    ]
\addplot+[ybar] plot coordinates {(p1,73) (p1,44)};
\addplot+[ybar] plot coordinates {(p2,45) (p2,76)
  (p2,68) (p2,99) };
\addplot+[ybar] plot coordinates {(p3,9) (p3,26)
  (p3,45) (p3,35)};
\end{axis}
\end{tikzpicture}
\end{document} 

2 Answers 2

2

One \addplot should not contain all values of e.g. P1, it should contain the three values for P1, P2 and P3 for one of the stacked bars, e.g.

\addplot+[ybar] plot coordinates {(P1,73) (P2,45) (P3,9)};

To stack the bars, create one such \addplot for every bar you want to add on top. Even though P1 only has two bars, you need to specify the coordinate and e.g. set it to zero:

\addplot+[ybar] plot coordinates {(P1,0) (P2,99) (P3,35)};

One more note: Use the same case for the symbolic coordinates, i.e. don't mix p1 and P1.

Your MWE then becomes

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ybar stacked,
    bar width=15pt,
    nodes near coords,
    enlargelimits=0.15,
    legend style={at={(0.5,-0.20)},
        anchor=north,legend columns=-1},
    ylabel={\#Schedule Length},
    symbolic x coords={P1, P2, P3},
    xtick=data,
    x tick label style={rotate=45,anchor=east},
    xlabel={\#Processor}
    ]

\addplot+[ybar] plot coordinates {(P1,73) (P2,45) (P3,9)};
\addplot+[ybar] plot coordinates {(P1,44) (P2,76) (P3,26)};
\addplot+[ybar] plot coordinates {(P1,0) (P2,68) (P3,45)};
\addplot+[ybar] plot coordinates {(P1,0) (P2,99) (P3,35)};

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

and the output:

resulting plot

If you want the second bar of P1 to be e.g. gray instead of red, you can set the second and third P1 value to zero, instead of the last two:

\addplot+[ybar] plot coordinates {(P1,73) (P2,45) (P3,9)};
\addplot+[ybar] plot coordinates {(P1,0) (P2,76) (P3,26)};
\addplot+[ybar] plot coordinates {(P1,0) (P2,68) (P3,45)};
\addplot+[ybar] plot coordinates {(P1,44) (P2,99) (P3,35)};

result2

1
  • thank you, but i need the exact my query representation type diagram.
    – KBM
    Dec 9, 2015 at 5:25
0
\begin{figure}[hbt]
  \centering
  \begin{tikzpicture}
    \begin{axis}[
        ybar stacked,
        nodes near coords,
        every node near coord/.append style=
{xshift=-15pt,yshift=-3pt,anchor=east,font=\footnotesize},
        xbar legend,
%         nodes near coords align={right},
        legend pos=outer north east,
        enlarge x limits={abs=1},
        enlarge y limits=false,
        bar width=35.5,width=10cm,height=11cm,
        % x axis
        xtick={1,2,3},
        xticklabels={p1,p2,p3},
        % y axis
        ymin=0,
        ylabel={Scheduling Length},
        xlabel={Processors},
        %yticklabels={0,10,20,30,40,50,60,70,80,90,500},
      ]
      \addplot table [
        x=index,
        y=blue,
      ] {
index   blue            pink    gray    orange
1       40              18      07      00
2       34              36      19      00
3       09              17      09      10
      };
      \addplot table [
        x=index,
        y=pink,
      ] {
index   blue            pink    gray    orange
1       40              18      07      00
2       34              36      19      00
3       09              17      09      10
};
      \addplot table [
      x=index,
      y=gray,
      ] {
index   blue            pink    gray    orange
1       40              18      07      00
2       34              36      19      00
3       09              17      09      10
      };
      \addplot table [
        x=index,
        y=orange,
      ] {
index   blue            pink    gray    orange
1       40              18      07      00
2       34              36      19      00
3       09              17      09      10
      };
      %\legend{First one,Second one,Third,Fourth}
    \end{axis}
  \end{tikzpicture}

\end{figure}

You must log in to answer this question.

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