在堆积条形图中添加 y 轴断点

在堆积条形图中添加 y 轴断点

我想在堆积条形图的 y 轴上添加一个断点。现在它看起来像这样:

在此处输入图片描述

如果它看起来与此相似,但没有错误,那就太好了:

在此处输入图片描述

这是 tex 代码:

\pgfplotstableread[col sep=comma,header=false]{
    All,192,37
    completed,61,11
    dropped,64,17
    in study,47,3
    changed,26,6
}\data

\pgfplotstablecreatecol[
    create col/expr={
        \thisrow{1} + \thisrow{2}
    }
]{sum}{\data}

\pgfplotsset{
    percentage plot/.style={
            point meta=explicit,
    every node near coord/.append style={
        xshift=17pt,
        yshift=-1.15pt,
        anchor=east,
%       font=\footnotesize,
        color=black,
                align=center,
            text width=1cm,
        },
        nodes near coords={
            \pgfmathtruncatemacro\iszero{\originalvalue==0}
            \ifnum\iszero=0
%       \pgfmathprintnumber{\originalvalue}$\,\%$\\
        \pgfmathprintnumber[fixed zerofill,precision=0]{\pgfplotspointmeta}
            \fi},
     nodes near coords align={vertical},
            yticklabel=\pgfmathprintnumber{\tick}\,$\%$,
            ymin=0,
            ymax=100,
%        enlarge y limits={upper,value=0.1},
        visualization depends on={y \as \originalvalue}
    },
    percentage series/.style={
        table/y expr=(\thisrow{#1}/\thisrow{sum}*100),table/meta=#1
    }
}

\begin{tikzpicture}
\begin{axis}[
        ybar stacked,
%       axis on top,
    height=8cm,
    %    width=10cm,
        percentage plot,
%   axis y discontinuity=crunch,
    bar width=0.85cm,
    symbolic x coords={All,completed,dropped,in study,changed},
        xtick=data,
    x tick label style={rotate=45, anchor=east, xshift=-1.5mm, yshift=-2mm},
%   ylabel={\#a},
%    enlargelimits=0.15,
     legend style={
        at={(0.5,-0.30)},
        anchor=south,
        legend columns=-2
    }
]
\addplot table [percentage series=1] {\data};
\addplot table [percentage series=2] {\data};
\legend{\strut Men, \strut Women}
\end{axis}
\end{tikzpicture}

答案1

请参阅 pgfplots 手册第 258 页。

实际上,您的代码也将标签放在顶部,而不是放在块的中心。

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}

\begin{document}
\pgfplotstableread[col sep=comma,header=false]{
    All,192,37
    completed,61,11
    dropped,64,17
    in study,47,3
    changed,26,6
}\data

\pgfplotstablecreatecol[
    create col/expr={
        \thisrow{1} + \thisrow{2}
    }
]{sum}{\data}

\pgfplotsset{
    percentage plot/.style={
            point meta=explicit,
    every node near coord/.append style={
        xshift=17pt,
        yshift=-1.15pt,
        anchor=east,
%       font=\footnotesize,
        color=black,
                align=center,
            text width=1cm,
        },
        nodes near coords={
            \pgfmathtruncatemacro\iszero{\originalvalue==0}
            \ifnum\iszero=0
%       \pgfmathprintnumber{\originalvalue}$\,\%$\\
        \pgfmathprintnumber[fixed zerofill,precision=0]{\pgfplotspointmeta}
            \fi},
     nodes near coords align={vertical},
            yticklabel=\pgfmathprintnumber{\tick}\,$\%$,
            ymax=100,
% start of changes ******************************************
            ymin=50,
            ytickmin=60,
            axis y discontinuity=crunch,
% end of changes ********************************************
%        enlarge y limits={upper,value=0.1},
        visualization depends on={y \as \originalvalue}
    },
    percentage series/.style={
        table/y expr=(\thisrow{#1}/\thisrow{sum}*100),table/meta=#1
    }
}

\begin{tikzpicture}
\begin{axis}[
        ybar stacked,
%       axis on top,
    height=8cm,
    %    width=10cm,
        percentage plot,
%   axis y discontinuity=crunch,
    bar width=0.85cm,
    symbolic x coords={All,completed,dropped,in study,changed},
        xtick=data,
    x tick label style={rotate=45, anchor=east, xshift=-1.5mm, yshift=-2mm},
%   ylabel={\#a},
%    enlargelimits=0.15,
     legend style={
        at={(0.5,-0.30)},
        anchor=south,
        legend columns=-2
    }
]
\addplot table [percentage series=1] {\data};
\addplot table [percentage series=2] {\data};
\legend{\strut Men, \strut Women}
\end{axis}
\end{tikzpicture}
\end{document}

演示

相关内容