pgfplots:绘制百分比总和的面积(类似'ybar stacked',但为面积样式)

pgfplots:绘制百分比总和的面积(类似'ybar stacked',但为面积样式)

抱歉,标题不对,但我不知道如何正确解释我的问题。

我想要一个填充面积图,类似于 pgfplots 手册第 4.4.9 章(面积图),但以总和百分比作为值。

因此,我想总结一下 2005 年等的所有百分比值。

效果ybar stacked很好,但我希望填充区域也能如此。

这是一个可以工作的最小示例,它首先创建 ybar 堆叠,然后尝试创建 ybar 堆叠区域,但没有按预期工作:

\documentclass[a4paper,11pt,twoside]{memoir}
\usepackage{pgf}                    
\usepackage{tikz}                   % tikz graphics
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepgfplotslibrary{dateplot}

\begin{document}

\pgfplotstableread[col sep=comma,header=true]{
Year,1,1.1,1.2,1.3,1.4
2005,0,0.29,21.97,42.22,27.98
2006,0,1.43,8.25,47.53,29.96
2007,0.05,0.05,13.62,45.26,34.05
2008,0.06,0.89,10.63,30.84,44.63
2009,0.09,0,7.71,30.82,46.75
2010,0,0,1.65,28.34,27.02
2011,0,0,0.94,29.02,7.64
}\data

\begin{figure} [tb]%
    \centering
    \begin{tikzpicture}     
        \begin{axis}[
                ybar stacked,
            grid=major,
            xlabel={Year},
            ylabel={\%},            
            enlarge y limits={abs=0},                       
            xtick=data,
            x tick label style={/pgf/number format/1000 sep=},          
        ]
        \addplot+[ybar, blue, fill=blue] table [x=Year, y=1] {\data};
        \addplot+[ybar] table [x=Year, y=1.1] {\data};
        \addplot+[ybar] table [x=Year, y=1.2] {\data};
        \addplot+[ybar] table [x=Year, y=1.3] {\data};
        \addplot+[ybar] table [x=Year, y=1.4] {\data};      
        \end{axis}
    \end{tikzpicture}
\caption{ybar stacked}
\label{fig:pdf-test2}
\end{figure}

\begin{figure} [tb]%
    \centering
    \begin{tikzpicture}     
        \begin{axis}[
                stack plots=y,
                area style,         
                xlabel={Year},
                ylabel={\%},            
                enlarge x limits=false,             
                legend pos=outer north east,            
                x tick label style={/pgf/number format/1000 sep=},          
            ]
            \addplot table [x=Year, y=1] {\data};
            \addplot table [x=Year, y=1.1] {\data};             
            \addplot table [x=Year, y=1.2] {\data};
            \addplot table [x=Year, y=1.3] {\data};
            \addplot table [x=Year, y=1.4] {\data};     
            \end{axis}
    \end{tikzpicture}
\caption{ybar stacked area}
\label{fig:pdf-test3}
\end{figure}

\end{document}

答案1

您几乎已经拥有它了,除了您缺少需要\closedcycle添加到\addplot命令末尾的命令以便正确填充曲线下的区域:

\begin{tikzpicture}     
    \begin{axis}[
            stack plots=y,
            area style,         
            xlabel={Year},
            ylabel={\%},            
            enlarge x limits=false,
            enlarge y limits=upper,         
            legend pos=outer north east,            
            x tick label style={/pgf/number format/1000 sep=},          
        ]
        \addplot table [x=Year, y=1] {\data} \closedcycle;
        \addplot table [x=Year, y=1.1] {\data} \closedcycle;             
        \addplot table [x=Year, y=1.2] {\data} \closedcycle;
        \addplot table [x=Year, y=1.3] {\data} \closedcycle;
        \addplot table [x=Year, y=1.4] {\data} \closedcycle;     
        \end{axis}
\end{tikzpicture}

附注:如果您提供ybar stacked选项axis,则无需添加ybar到每个系列。这是自动设置的。

相关内容