几乎就像你的例子一样

几乎就像你的例子一样

我尝试了堆积图,但我需要在内部栏设置字母数字,如 p1、p2、p3。

我的MWE

\documentclass{article}
\usepackage{pgfplots}          % to print charts
%\pgfplotsset{compat=1.8}
%\pgfplotsset{compat=1.7}
\begin{document}
\begin{figure}
  \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=13cm,
        % 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       44              29      00      00
2       45              23      08      23
3       09              17      09      10      
      };
      \addplot table [
        x=index,
        y=pink,
      ] {
index   blue            pink    gray    orange
1       44              29      00      00
2       45              23      08      23
3       09              17      09      10 
};
      \addplot table [
      x=index,
      y=gray,
      ] {
index   blue            pink    gray    orange
1       44              29      00      00
2       45              23      08      23
3       09              17      09      10 
      };
      \addplot table [
        x=index,
        y=orange,
      ] {
index   blue            pink    gray    orange
1       44              29      00      00
2       45              23      08      23
3       09              17      09      10
      };
      %\legend{First one,Second one,Third,Fourth}
    \end{axis}
  \end{tikzpicture}
  \caption{Makespan for PETS}
\end{figure}
\end{document}

下图中的数字栏就是我需要的。例如 P1 [ 2,8]

在此处输入图片描述

答案1

(我仍然不完全确定你到底想要什么,所以如果这与目标相差甚远,请发表评论,如果可以的话我会改变它。这里有四个不同的部分,每个部分都有不同的设置。)

几乎就像你的例子一样

在此处输入图片描述

\documentclass{article}
\usepackage{pgfplots}          % to print charts
\begin{document}
\begin{figure}
  \centering
  \pgfplotsset{
    /pgfplots/bar cycle list/.style={/pgfplots/cycle list={%
        {black,fill=white,mark=none},%
        {black,fill=white,mark=none},%
        {black,fill=white,mark=none},%
        {black,fill=white,mark=none},%
        }
    },
}
  \begin{tikzpicture}
    \begin{axis}[
        ybar stacked,
        point meta=explicit symbolic,
        nodes near coords=\pgfplotspointmeta,
        every node near coord/.append style=
{anchor=north,fill=gray,minimum width=20pt,black},
        xbar legend,
%         nodes near coords align={right},
        legend pos=outer north east,
        enlarge x limits={abs=1},
        enlarge y limits=false,
        bar width=20pt,width=10cm,height=13cm,
        % x axis
        xtick={1,2,3},
        xticklabels={p1,p2,p3},
        % y axis
        ymin=0,
        ylabel={Scheduling Length},
        xlabel={Processors},
      ]
      \addplot table [
        x=index,
        y=blue,
       meta=meta
      ] {
index   blue            pink    gray    orange   meta
1       44              29      00      00       1
2       45              23      08      23       2
3       09              17      09      10       3
      };
      \addplot table [
        x=index,
        y=pink,
        meta=meta
      ] {
index   blue            pink    gray    orange  meta
1       44              29      00      00      4
2       45              23      08      23      5
3       09              17      09      10      6
};
      \addplot table [
      x=index,
      y=gray,
      meta=meta
      ] {
index   blue            pink    gray    orange  meta
1       44              29      00      00      \space
2       45              23      08      23      8
3       09              17      09      10      9
      };
      \addplot table [
        x=index,
        y=orange,
        meta=meta
      ] {
index   blue            pink    gray    orange   meta
1       44              29      00      00       \space
2       45              23      08      23       11
3       09              17      09      10       12
      };
      %\legend{First one,Second one,Third,Fourth}
    \end{axis}
  \end{tikzpicture}
  \caption{Makespan for PETS}
\end{figure}
\end{document}

累计值

这基本上就是您所拥有的。只需更改样式即可every node near coord将其移入栏中。由于您要多次阅读同一张表,因此先将其保存到宏中会更方便。

\documentclass{article}
\usepackage{pgfplots}          % to print charts
\pgfplotstableread{
index   blue            pink    gray    orange
1       44              29      00      00      
2       45              23      08      23      
3       09              17      09      10  
}\DataForBars
\begin{document}
\begin{figure}
  \centering
  \begin{tikzpicture}
    \begin{axis}[
        ybar stacked,
        nodes near coords,
        every node near coord/.append style=
{anchor=north},
        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=13cm,
        % x axis
        xtick={1,2,3},
        xticklabels={p1,p2,p3},
        % y axis
        ymin=0,
        ylabel={Scheduling Length},
        xlabel={Processors},
      ]
      \addplot table [
        x=index,
        y=blue,
      ] {\DataForBars};
      \addplot table [
        x=index,
        y=pink,
      ] {\DataForBars};
      \addplot table [
      x=index,
      y=gray,
      ] {\DataForBars};
      \addplot table [
        x=index,
        y=orange,
      ] {\DataForBars};
      %\legend{First one,Second one,Third,Fourth}
    \end{axis}
  \end{tikzpicture}
  \caption{Makespan for PETS}
\end{figure}
\end{document}

在此处输入图片描述

堆栈各部分的值

对于compat=1.9较新的版本,默认行为是将“堆栈”每个部分的高度放在栏的中间,因此添加类似的东西就\pgfplotsset{compat=1.12}可以实现这一点。

在此处输入图片描述

\documentclass{article}
\usepackage{pgfplots}          % to print charts
\pgfplotsset{compat=1.12}
\pgfplotstableread{
index   blue            pink    gray    orange
1       44              29      00      00      
2       45              23      08      23      
3       09              17      09      10  
}\DataForBars
\begin{document}
\begin{figure}
  \centering
  \begin{tikzpicture}
    \begin{axis}[
        ybar stacked,
        nodes near coords,
        xbar legend,
        legend pos=outer north east,
        enlarge x limits={abs=1},
        enlarge y limits=false,
        bar width=0.5,
        width=10cm,height=13cm,
        % x axis
        xtick={1,2,3},
        xticklabels={p1,p2,p3},
        % y axis
        ymin=0,
        ylabel={Scheduling Length},
        xlabel={Processors},
      ]
      \addplot table [
        x=index,
        y=blue,
      ] {\DataForBars};
      \addplot table [
        x=index,
        y=pink,
      ] {\DataForBars};
      \addplot table [
      x=index,
      y=gray,
      ] {\DataForBars};
      \addplot table [
        x=index,
        y=orange,
      ] {\DataForBars};
      %\legend{First one,Second one,Third,Fourth}
    \end{axis}
  \end{tikzpicture}
  \caption{Makespan for PETS}
\end{figure}
\end{document}

自定义文本

通过添加point meta=explicit symbolic,nodes near coords=\pgfplotspointmeta轴选项和表格中的新列(此处称为meta),您可以向条形图添加自定义文本。\space您看到的几个地方只是一种技巧,可以避免由于表格中的零值而导致的值重叠。

在此处输入图片描述

\documentclass{article}
\usepackage{pgfplots}          % to print charts
\begin{document}
\begin{figure}
  \centering
  \begin{tikzpicture}
    \begin{axis}[
        ybar stacked,
        point meta=explicit symbolic,
        nodes near coords=\pgfplotspointmeta,
        every node near coord/.append style=
{anchor=north},
        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=13cm,
        % x axis
        xtick={1,2,3},
        xticklabels={p1,p2,p3},
        % y axis
        ymin=0,
        ylabel={Scheduling Length},
        xlabel={Processors},
      ]
      \addplot table [
        x=index,
        y=blue,
       meta=meta
      ] {
index   blue            pink    gray    orange   meta
1       44              29      00      00       1
2       45              23      08      23       2
3       09              17      09      10       3
      };
      \addplot table [
        x=index,
        y=pink,
        meta=meta
      ] {
index   blue            pink    gray    orange  meta
1       44              29      00      00      \space
2       45              23      08      23      5
3       09              17      09      10      6
};
      \addplot table [
      x=index,
      y=gray,
      meta=meta
      ] {
index   blue            pink    gray    orange  meta
1       44              29      00      00      \space
2       45              23      08      23      8
3       09              17      09      10      9
      };
      \addplot table [
        x=index,
        y=orange,
        meta=meta
      ] {
index   blue            pink    gray    orange   meta
1       44              29      00      00       10
2       45              23      08      23       11
3       09              17      09      10       12
      };
      %\legend{First one,Second one,Third,Fourth}
    \end{axis}
  \end{tikzpicture}
  \caption{Makespan for PETS}
\end{figure}
\end{document}

相关内容