瀑布图不层叠

瀑布图不层叠

这是 MWE

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots, pgfplotstable}
\usepackage{filecontents}

\pgfplotsset{compat=1.5.1}

\begin{filecontents}{datatable.csv}
27  47
12  21
39  69
12 17
17 23
67 69
\end{filecontents}


\pgfplotstableset{
    create on use/accumyprev/.style={
        create col/expr={\prevrow{0}+\prevrow{1}+\pgfmathaccuma}
    }
}


% Style for centering the labels
\makeatletter
\pgfplotsset{
    centered nodes near coords/.style={
    calculate offset/.code={
        \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
        \pgfmathsetmacro\testmacro{(\pgfplotspointmeta*10^\pgfplots@data@scale@trafo@EXPONENT@y)/2*\pgfplots@y@veclength)}
        \pgfkeys{/pgf/fpu=false}
    },
    every node near coord/.style={
        /pgfplots/calculate offset,
        yshift=-\testmacro,
        black,
        font=\scriptsize,
    },
    nodes near coords align=center
    }
}
\makeatother


\begin{document}

\begin{tikzpicture}
\begin{axis}[
width  = 0.85*\textwidth,
        height = 8cm,
    no markers,
    ybar stacked,
    bar width = 20pt,
    ymin=0,
    point meta=explicit,
    centered nodes near coords, 
    axis lines*=left,
    xtick=data,
    major tick length=0pt,
    xticklabels={
    2014\\ GDP,
        Business \\as \\ usual \\growth,
        Total \\2025 \\ business-\\as-usual \\l GDP,
        Increm-\\ental \\ best-\\in-region\\  GDP\\ in 2025,
        additional\\ GDP \\ in full\\-potential \\ scenarios in\\  2025,
        Total \\2025\\ business-\\as-usual\\ GDP,
    },
    xticklabel style={font=\small, text width=4cm, align=center},
    enlarge x limits=-0.85,
    ytick=\empty,
    y axis line style={opacity=0},
    ylabel style={font=\small},
    axis on top
]

% The first plot sets the "baseline": Uses the sum of all previous y values, except for the last bar, where it becomes 0
\addplot +[
    y filter/.code={\ifnum\coordindex>2 \def\pgfmathresult{0}\fi},
    draw=none,
    fill=none
] table [x expr=\coordindex, y=accumyprev] {datatable.csv};

% The lower bound
\addplot +[
    fill=orange,
    draw=orange,
    ybar stacked,
    nodes near coords
] table [x expr=\coordindex, y index=0, meta index=0] {datatable.csv};

% The upper bound
\addplot +[
    ybar stacked,
    draw=orange!50,
    fill=orange!50,
    nodes near coords
] table [x expr=\coordindex, y index=1, meta index=1] {datatable.csv};

% The connecting line. Uses a bit of magic to typeset the ranges
\addplot [
    const plot, black,
    point meta={
        TeX code symbolic={
            \pgfkeys{/pgf/fpu/output format=fixed}
            \pgfmathtruncatemacro\upperbound{
                \thisrowno{0} + \thisrowno{1}
            }
            \edef\dostuff{
                \noexpand\def\noexpand\pgfplotspointmeta{%
                    \thisrowno{0}--\upperbound%
                }
            }%
            \dostuff
        }
    },
    nodes near coords=\pgfplotspointmeta,
    every node near coord/.style={
        font=\scriptsize,
        anchor=south
    },
] table [x expr=\coordindex, y expr=0] {datatable.csv};
\end{axis}
\end{tikzpicture}
\end{document}

答案1

顺便说一句,如果你提到你从哪里得到这个,那就太好了瀑布图。我认为,只有两件事是错误的。

  1. 数据文件中的最后一行应该是以上各行的总和,所以107 177,而不是61 69

  2. 在第一个图中,您有一个\ifnum\coordindex>2应该更改为的\ifnum\coordindex>4\coordindex从零开始计数,并且您希望最后一条线的基线为 0,而不是累积和。

我还将enlarge x limits设置更改为enlarge x limits={abs=0.5}

代码输出

\documentclass[border=5mm]{standalone}
\usepackage{pgfplotstable}
\usepackage{filecontents}

\pgfplotsset{compat=1.5.1}

\begin{filecontents}{datatable.csv}
27  47
12  21
39  69
12 17
17 23
107 177
\end{filecontents}


\pgfplotstableset{
    create on use/accumyprev/.style={
        create col/expr={\prevrow{0}+\prevrow{1}+\pgfmathaccuma}
    }
}


% Style for centering the labels
\makeatletter
\pgfplotsset{
    centered nodes near coords/.style={
    calculate offset/.code={
        \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
        \pgfmathsetmacro\testmacro{(\pgfplotspointmeta*10^\pgfplots@data@scale@trafo@EXPONENT@y)/2*\pgfplots@y@veclength)}
        \pgfkeys{/pgf/fpu=false}
    },
    every node near coord/.style={
        /pgfplots/calculate offset,
        yshift=-\testmacro,
        black,
        font=\scriptsize,
    },
    nodes near coords align=center
    }
}
\makeatother


\begin{document}

\begin{tikzpicture}
\begin{axis}[
width  = 0.85*\textwidth,
        height = 8cm,
    no markers,
    ybar stacked,
    bar width = 20pt,
    ymin=0,
    point meta=explicit,
    centered nodes near coords, 
    axis lines*=left,
    xtick=data,
    major tick length=0pt,
    xticklabels={
    2014\\ GDP,
        Business \\as \\ usual \\growth,
        Total \\2025 \\ business-\\as-usual \\l GDP,
        Increm-\\ental \\ best-\\in-region\\  GDP\\ in 2025,
        additional\\ GDP \\ in full\\-potential \\ scenarios in\\  2025,
        Total \\2025\\ business-\\as-usual\\ GDP,
    },
    xticklabel style={font=\small, text width=4cm, align=center},
    enlarge x limits={abs=0.5},
    ytick=\empty,
    y axis line style={opacity=0},
    ylabel style={font=\small},
    axis on top
]

% The first plot sets the "baseline": Uses the sum of all previous y values, except for the last bar, where it becomes 0
\addplot +[
    y filter/.code={\ifnum\coordindex>4 \def\pgfmathresult{0}\fi},
    draw=none,
    fill=none
] table [x expr=\coordindex, y=accumyprev] {datatable.csv};

% The lower bound
\addplot +[
    fill=orange,
    draw=orange,
    ybar stacked,
    nodes near coords
] table [x expr=\coordindex, y index=0, meta index=0] {datatable.csv};

% The upper bound
\addplot +[
    ybar stacked,
    draw=orange!50,
    fill=orange!50,
    nodes near coords
] table [x expr=\coordindex, y index=1, meta index=1] {datatable.csv};

% The connecting line. Uses a bit of magic to typeset the ranges
\addplot [
    const plot, black,
    point meta={
        TeX code symbolic={
            \pgfkeys{/pgf/fpu/output format=fixed}
            \pgfmathtruncatemacro\upperbound{
                \thisrowno{0} + \thisrowno{1}
            }
            \edef\dostuff{
                \noexpand\def\noexpand\pgfplotspointmeta{%
                    \thisrowno{0}--\upperbound%
                }
            }%
            \dostuff
        }
    },
    nodes near coords=\pgfplotspointmeta,
    every node near coord/.style={
        font=\scriptsize,
        anchor=south
    },
] table [x expr=\coordindex, y expr=0] {datatable.csv};
\end{axis}
\end{tikzpicture}
\end{document}

相关内容