pgfplot/tikz 堆积条形图,兼容 1.9 或更高版本

pgfplot/tikz 堆积条形图,兼容 1.9 或更高版本

受这个问题的启发,pgfplot/tikz 堆积条形图,我一直在尝试创建一个堆叠条形图,并在堆叠条形的末尾添加数字。但是,nodes near coords在 pgfplot v1.9 中,的位置已更改为条形段的中心。我尝试了几种方法,包括设置nodes near coords style={text depth=2cm},但这显然不会随数据值移动。我也尝试过制作“topper”系列,但在我看来这不是最好的解决方案,而且会造成混乱reverse legend=true(不过,可以通过更手动指定的图例来修复)。

在 1.9 之前版本可以工作:

\documentclass{standalone}
\usepackage{tikz,pgfplots,pgfplotstable}
\pgfplotsset{compat=1.9} % set to 1.8 to get old behaviour

\begin{document}

    \pgfplotstableread{
Label series1 series2 series3
A 21.87797  5.566584 24.85545
B 20.93009  7.937881 24.33203
C 20.41148 21.126529 27.56199
D 26.83072 23.823827 28.94545
    }\testdata

    \begin{tikzpicture}

    \begin{axis}[
        ybar stacked,
        ymin=0,
        ymax=100,
        xtick=data,
        legend style={cells={anchor=west}, legend pos=north west},
        reverse legend=true,
        xticklabels from table={\testdata}{Label},
        xticklabel style={text width=2cm,align=center},
    ]
    \addplot [fill=green!80] table [y=series1, meta=Label, x expr=\coordindex] {\testdata};
    \addlegendentry{series1}
    \addplot [fill=blue!60] table [y=series2, meta=Label, x expr=\coordindex] {\testdata};
    \addlegendentry{series2}
    \addplot [fill=red!60,nodes near coords,point meta=y] table [y=series3, meta=Label, x expr=\coordindex] {\testdata};
    \addlegendentry{series3}

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

包含最后一段总计的堆叠条形图

“顶级”解决方案:

\documentclass{standalone}
\usepackage{tikz,pgfplots,pgfplotstable}
\pgfplotsset{compat=1.9} % set to 1.8 to get old behaviour

\begin{document}

    \pgfplotstableread{
Label series1 series2 series3 topper
A 21.87797  5.566584 24.85545 0.001
B 20.93009  7.937881 24.33203 0.001
C 20.41148 21.126529 27.56199 0.001
D 26.83072 23.823827 28.94545 0.001
    }\testdata

    \begin{tikzpicture}

    \begin{axis}[
        ybar stacked,
        ymin=0,
        ymax=100,
        xtick=data,
        legend style={cells={anchor=west}, legend pos=north west},
        reverse legend=true, % set to false to get correct display, but I'd like to have this true
        xticklabels from table={\testdata}{Label},
        xticklabel style={text width=2cm,align=center},
    ]
    \addplot [fill=green!80] table [y=series1, meta=Label, x expr=\coordindex] {\testdata};
    \addlegendentry{series1}
    \addplot [fill=blue!60] table [y=series2, meta=Label, x expr=\coordindex] {\testdata};
    \addlegendentry{series2}
    \addplot [fill=red!60] table [y=series3, meta=Label, x expr=\coordindex] {\testdata};
    \addlegendentry{series3}
    \addplot [nodes near coords,point meta=y,nodes near coords style={anchor=south}] table [y=topper, meta=Label, x expr=\coordindex] {\testdata};

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

总计高于最后一段的堆叠条形图,使用顶部解决方案

答案1

最简单的解决方案是将compat/bar nodes级别重新设置为1.8。通过这样做,您可以使用按compat级别设置的所有新功能(明确设置的单项compat/...功能除外)。

% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
    \pgfplotsset{
        compat=1.9,
        compat/bar nodes=1.8,
    }
    \pgfplotstableread{
        Label series1 series2 series3
        A 21.87797  5.566584 24.85545
        B 20.93009  7.937881 24.33203
        C 20.41148 21.126529 27.56199
        D 26.83072 23.823827 28.94545
    }\testdata
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        ybar stacked,
        ymin=0,
        ymax=100,
        xtick=data,
        legend style={
            cells={anchor=west},
            legend pos=north west,
        },
        reverse legend=true,
        xticklabels from table={\testdata}{Label},
        xticklabel style={text width=2cm,align=center},
    ]
        \addplot [fill=green!80]
            table [y=series1, meta=Label, x expr=\coordindex]
                {\testdata};
                    \addlegendentry{series1}
        \addplot [fill=blue!60]
            table [y=series2, meta=Label, x expr=\coordindex]
                {\testdata};
                    \addlegendentry{series2}
        \addplot [fill=red!60,nodes near coords,point meta=y]
            table [y=series3, meta=Label, x expr=\coordindex]
                {\testdata};
                    \addlegendentry{series3}
    \end{axis}
\end{tikzpicture}
\end{document}

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

答案2

可以稍微改进一下“topper”解决方案,通过将键设置为一个小的常量来消除必须有一个包含接近零数据的表列的要求y expr。不过,图例仍然有问题。

\documentclass{standalone}
\usepackage{biblatex} % so can use same script/recipie as actual documents
\usepackage{tikz,pgfplots,pgfplotstable}

\begin{document}

    \pgfplotstableread{
Label series1 series2 series3
A 21.87797  5.566584 24.85545
B 20.93009  7.937881 24.33203
C 20.41148 21.126529 27.56199
D 26.83072 23.823827 28.94545
    }\testdata

    \begin{tikzpicture}

    \begin{axis}[
        ybar stacked,
        ymin=0,
        ymax=100,
        xtick=data,
        legend style={cells={anchor=west}, legend pos=north west},
        reverse legend=true, % set to false to get correct display, but I'd like to have this true
        xticklabels from table={\testdata}{Label},
        xticklabel style={text width=2cm,align=center},
    ]
    \addplot [fill=green!80] table [y=series1, meta=Label, x expr=\coordindex] {\testdata};
    \addlegendentry{series1}
    \addplot [fill=blue!60] table [y=series2, meta=Label, x expr=\coordindex] {\testdata};
    \addlegendentry{series2}
    \addplot [fill=red!60] table [y=series3, meta=Label, x expr=\coordindex] {\testdata};
    \addlegendentry{series3}
    \addplot [
        ybar, % this makes it show the total for some reason
        nodes near coords,
        nodes near coords style={%
            anchor=south,%
        },
    ] table [ y expr=0.00001, x expr=\coordindex] {\testdata};

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

生成的图表看起来正确,标签位于堆叠条形图上方,但图例显示了错误的颜色

相关内容