如何绘制堆积面积图

如何绘制堆积面积图

我想绘制堆积面积图。堆积面积图 => 所有值都堆积起来,线下的区域被着色。

像这样: 在此处输入图片描述

我的数据是一个简单示例:

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\pgfplotsset{compat=1.12}
\usetikzlibrary{fillbetween}
\usepackage{filecontents}

\begin{filecontents*}{data.csv}
date,backlog,wip,finished
2015-01-06,54,27,3
2015-01-13,55,27,5
2015-01-20,55,27,5
2015-01-27,54,27,8
2015-02-03,54,27,8
2015-02-10,56,27,10
2015-02-17,56,25,12
2015-02-24,63,24,17
2015-03-02,63,21,17
2015-03-09,59,23,20
2015-03-16,59,25,21
2015-03-23,55,27,26
2015-03-30,55,30,26
2015-04-06,62,28,30
2015-04-13,62,28,30
2015-04-20,65,22,40
2015-04-27,65,22,40
2015-05-04,61,22,44
2015-05-11,61,20,47
2015-05-18,60,21,50
2015-05-25,59,21,50
\end{filecontents*}
\begin{document}
    \begin{tikzpicture}[]
    \begin{axis}[
    date coordinates in=x, 
    table/col sep=comma, 
    date ZERO=2015-01-06, 
    xticklabel={\day.\month.\year}, 
    xticklabel style={rotate=90, anchor=near xticklabel},
    xmin={2015-01-06}, 
    xmax={2015-05-25},
    ymin=0,
    ymax=140,
    max space between ticks=20
    ]

    \addplot table [mark=none,x=date,y=backlog] {data.csv};
    \addplot table [mark=none,x=date,y=wip] {data.csv};
    \addplot table [mark=none,x=date,y=finished] {data.csv};

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

答案1

在您的参数中axes您需要添加area style每个图的结束宽度\closedcycle

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{dateplot}
\pgfplotsset{compat=1.12}
\usetikzlibrary{fillbetween}
\usepackage{filecontents}

\begin{filecontents*}{data.csv}
date,backlog,wip,finished
2015-01-06,54,27,3
2015-01-13,55,27,5
2015-01-20,55,27,5
2015-01-27,54,27,8
2015-02-03,54,27,8
2015-02-10,56,27,10
2015-02-17,56,25,12
2015-02-24,63,24,17
2015-03-02,63,21,17
2015-03-09,59,23,20
2015-03-16,59,25,21
2015-03-23,55,27,26
2015-03-30,55,30,26
2015-04-06,62,28,30
2015-04-13,62,28,30
2015-04-20,65,22,40
2015-04-27,65,22,40
2015-05-04,61,22,44
2015-05-11,61,20,47
2015-05-18,60,21,50
2015-05-25,59,21,50
\end{filecontents*}
\begin{document}
    \begin{tikzpicture}[]
    \begin{axis}[
    date coordinates in=x,
    table/col sep=comma,
    date ZERO=2015-01-06,
    xticklabel={\day.\month.\year},
    xticklabel style={rotate=90, anchor=near xticklabel},
    xmin={2015-01-06},
    xmax={2015-05-25},
    ymin=0,
    ymax=140,
    max space between ticks=20,
    stack plots=y,%   
    area style,
    ]
\addplot table [mark=none,x=date,y=backlog] {data.csv}
    \closedcycle;
\addplot table [mark=none,x=date,y=wip] {data.csv}
    \closedcycle;
\addplot table [mark=none,x=date,y=finished] {data.csv}
    \closedcycle;
    \end{axis}
    \end{tikzpicture}
\end{document}

图书馆fillbetween不是必需的,但如果存在的话也无害。

编辑:如果您想更改区域颜色列表,请执行以下操作:

\pgfplotsset{
/pgfplots/area cycle list/.style={/pgfplots/cycle list={%
{red,fill=blue!30!white,mark=none},%
{blue,fill=red!30!white,mark=none},%
{yellow!60!black,fill=yellow!30!white,mark=none},%
{black,fill=gray,mark=none},
}
},
}

从此列表可以看出,不需要\addplot添加参数:marks=none。这就足够了:

\addplot table [x=date,y=backlog] {data.csv}
    \closedcycle;
\addplot table [x=date,y=wip] {data.csv}
    \closedcycle;
\addplot table [x=date,y=finished] {data.csv}
    \closedcycle;

相关内容