如何绘制堆积条形图?

如何绘制堆积条形图?

我尝试使用 pgfplots 包创建具有给定值的堆积条形图,但它显示一些错误。有人可以纠正给定的错误吗?

在此处输入图片描述

\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} 

答案1

一个\addplot不应包含所有的值P1,例如,它应该包含的三个值P1P2以及P3其中一个堆叠条形图的值,例如

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

要堆叠条形图,\addplot请为要添加在顶部的每个条形图创建一个这样的条形图。即使P1只有两个条形图,您也需要指定坐标并将其设置为零:

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

还有一点需要注意:对符号坐标使用相同的大小写,即不要混合p1P1

你的 MWE 就会变成

\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} 

输出:

结果图

如果您希望第二条条形图P1为灰色而不是红色,则可以将第二个和第三个P1值设置为零,而不是最后两个值:

\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)};

结果2

答案2

\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}

相关内容