使用 pgfplot 填充堆叠线之间的区域

使用 pgfplot 填充堆叠线之间的区域

在此处输入图片描述

我想在 pgfplots 中重新创建上图。不幸的是,如果我使用stacked plots选项

%%%%%%%%%%% MISC
\usepackage[utf8]{inputenc}
\pagestyle{empty} % Suppresses page number

%%%%%%%%%%% PLOTTING
\usepackage{tikz} % Engine for pgfplots
\usepackage{pgfplots} % For inline plotting
%\pgfplotsset{compat=1.15} % For pgfplot backwards compatibility

\pgfplotsset{compat=newest,
        width=10cm,
        height=7cm,
        xtick pos=left,
        ytick pos=left
        }

\begin{document}

\begin{tikzpicture}
\begin{axis}[
            stack plots=y,
            area style,
            enlarge x limits=false
            ]
\addplot [name path=A, blue] table [x=Year, y=Coal, col sep=comma] {data.csv};
\addplot [name path=B, red] table [x=Year, y=Oil, col sep=comma] {data.csv};
\addplot [grey] fill between[of=A and B];
\end{axis}
\end{tikzpicture}

\end{document}

我只收到来自fillbetween包的一个错误:

! 软件包 pgf 错误:填充之间:强制参数 'of= 和 缺失或参数为空。请确保已设置选项并且已分配两个路径名(也许您需要在某处使用 'name path global=A'?)

它适用于

%%%%%%%%%%% MISC
\usepackage[utf8]{inputenc}
\pagestyle{empty} % Suppresses page number

%%%%%%%%%%% PLOTTING
\usepackage{tikz} % Engine for pgfplots
\usepackage{pgfplots} % For inline plotting
%\pgfplotsset{compat=1.15} % For pgfplot backwards compatibility

\pgfplotsset{compat=newest,
        width=10cm,
        height=7cm,
        xtick pos=left,
        ytick pos=left
        }

\begin{document}

\begin{tikzpicture}
\begin{axis}[]
\addplot [name path=A, blue] table [x=Year, y=Coal, col sep=comma] {data.csv};
\addplot [name path=B, red] table [x=Year, y=Oil, col sep=comma] {data.csv};
\addplot [grey] fill between[of=A and B];
\end{axis}
\end{tikzpicture}

\end{document}

但这违背了我绘制图表的目的。我怎样才能得到我想要的输出?

答案1

实际上,您不需要该fillbetween库来实现您想要的功能。有关详细信息,请查看代码中的注释。

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
% "simple" version when the fill color equals the draw color
\begin{tikzpicture}
    \begin{axis}[
        stack plots=y,
        no markers,
        % options that are common to all `\addplot` commands
        every axis plot/.append style={
            % add the fill color to each `\addplot`
            % (`.` is an abbreviation for the "current color")
            fill=.,
        },
    ]
        % appended `\closedcycle` to each `\addplot` command
        \addplot coordinates {(0,1) (1,1) (2,2) (3,2)} \closedcycle;
        \addplot coordinates {(0,1) (1,1) (2,2) (3,2)} \closedcycle;
        \addplot coordinates {(0,1) (1,1) (2,2) (3,2)} \closedcycle;
    \end{axis}
\end{tikzpicture}

% not so simple version when the fill color does not equal the draw color
\begin{tikzpicture}
    % first draw the filled areas in a plot
    \begin{axis}[
        stack plots=y,
        no markers,
        every axis plot post/.append style={
            % here we don't want to draw the lines ...
            draw=none,
            % but only the (filled) areas
            fill=.!25,
        },
        % we don't want to draw the axis lines etc. twice
        axis lines=none,
    ]
        \addplot coordinates {(0,1) (1,1) (2,2) (3,2)} \closedcycle;
        \addplot coordinates {(0,1) (1,1) (2,2) (3,2)} \closedcycle;
        \addplot coordinates {(0,1) (1,1) (2,2) (3,2)} \closedcycle;
    \end{axis}
    % second draw the lines in a plot
    \begin{axis}[
        stack plots=y,
        no markers,
    ]
        \addplot coordinates {(0,1) (1,1) (2,2) (3,2)};
        \addplot coordinates {(0,1) (1,1) (2,2) (3,2)};
        \addplot coordinates {(0,1) (1,1) (2,2) (3,2)};
    \end{axis}
\end{tikzpicture}
\end{document}

该图显示了上述代码的结果

相关内容