调整条形图中重叠的百分比数字

调整条形图中重叠的百分比数字

我希望实现百分比数字不会像本例中的图表那样重叠(示例链接)因此数字应位于 A 和 B 之间的空格中(最好分别位于条形图前面)。此外,我希望所有数字都位于图表顶部(我认为这样看起来更好。):

例子

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
\pgfplotsset{testbar3/.style={
        xbar stacked,
        width=.8\textwidth,
        xmajorgrids = true,
        xmin=0,xmax=100,
        ytick = data, yticklabels = {B,A},
        tick align = outside, xtick pos = left,
        bar width=6mm, y=8mm,
        nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%}, % <-- prints % sign after y coordinate value
        xticklabel={\pgfmathprintnumber{\tick}\%},% <-- prints % sign after x tick value
        nodes near coords align={center}, % <-- horizontal alignment centered of nodes 
        enlarge y limits=0.5, % <-- Adds vertical space so to not crop the bars
}}
\begin{figure}
    \centering
    \begin{tikzpicture}
        \begin{axis}[testbar3, legend style={at={(0.5,-0.6)}, anchor=north,legend columns=-1},
            /tikz/every even column/.append style={column sep=0.2cm}
            ]
            \addplot coordinates{(20.00,1) (18.45,2)};
            \addplot coordinates{(28.57,1) (28.58,2)};
            \addplot coordinates{(11.43,1) (10.85,2)};
            \addplot coordinates{(28.57,1) (28.01,2)};
            \addplot coordinates{(6.43,1) (7.55,2)};
            \addplot coordinates{(5.00,1) (6.56,2)};
            \legend{A,B,A\textsubscript{1},B\textsubscript{1},A\textsubscript{2},C}
        \end{axis}
    \end{tikzpicture}
    \caption{Vergleich der prozentualen Ausdehnung:\\ A, B}
\end{figure}
\end{document}

调整后的图表:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
\pgfplotsset{testbar3/.style={
        xbar stacked,
        width=.8\textwidth,
        xmajorgrids = true,
        xmin=0,xmax=100,
        ytick = data, yticklabels = {B,A},
        tick align = outside, xtick pos = left,
        bar width=6mm, y=16mm,
        nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%}, % <-- prints % sign after y coordinate value
        xticklabel={\pgfmathprintnumber{\tick}\%},% <-- prints % sign after x tick value
        nodes near coords align={center}, % <-- horizontal alignment centered of nodes 
        enlarge y limits=0.75, % <-- Adds vertical space so to not crop the bars
}}
\begin{figure}
    \centering
    \begin{tikzpicture}
        \begin{axis}[testbar3, legend style={at={(0.5,-0.22)}, anchor=north,legend columns=-1},
            /tikz/every even column/.append style={column sep=0.2cm}, every node near coord/.append style={yshift=0.5cm}
            ]
            \addplot coordinates{(20.00,1) (18.45,2)};
            \addplot coordinates{(28.57,1) (28.58,2)};
            \addplot coordinates{(11.43,1) (10.85,2)};
            \addplot coordinates{(28.57,1) (28.01,2)};
            \addplot coordinates{(6.43,1) (7.55,2)};
            \addplot coordinates{(5.00,1) (6.56,2)};
            \legend{A,B,A\textsubscript{1},B\textsubscript{1},A\textsubscript{2},\textit{Coda}}
        \end{axis}
    \end{tikzpicture}
    \caption{Vergleich der prozentualen Ausdehnung:\\ A, B}
\end{figure}
\end{document}

需要做出改变

答案1

此解决方案向上或向下移动选定nodes near coords

X

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\pgfplotsset{,
    xmin=0,
    /tikz/font=\footnotesize,
}
\begin{document}
    \begin{figure}
    \centering
    \begin{tikzpicture}
    \begin{axis}[
        height=85pt,
        width=\axisdefaultwidth,
        xmajorgrids = true,
        xmin=0,xmax=100,
        ytick = data, yticklabels = {1,2},
        xtick={0,20,...,100},
        tick align = outside, xtick pos = left,
        x post scale=1.3,   
        scale only axis,
        enlarge y limits=0.75, 
        xbar stacked,
        bar width=6mm, y=14mm,
        ytick=data,
        nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%},
        nodes near coords style={
            yshift={
                ifthenelse(or(\plotnum == 5,\plotnum == 2) , -13pt,13pt)
            }
             }, % shift down the third and sixth node       
        xticklabel={\pgfmathprintnumber{\tick}\%},% <-- prints % sign after x tick value
        legend style={at={(0.5,-0.3)}, anchor=north,legend columns=-1,font=\normalsize},
        ]
           \addplot coordinates{(20.00,1) (18.45,2)};
            \addplot coordinates{(28.57,1) (28.58,2)};
            \addplot coordinates{(11.43,1) (10.85,2)};
            \addplot coordinates{(28.57,1) (28.01,2)};
            \addplot coordinates{(6.43,1) (7.55,2)};
            \addplot coordinates{(5.00,1) (6.56,2)};
            \legend{A,B,A\textsubscript{1},B\textsubscript{1},A\textsubscript{2},\textit{Coda}}     
        \end{axis}
    \end{tikzpicture}
     \caption{Vergleich der prozentualen Ausdehnung:  A, B}
    \end{figure}
\end{document}

相关内容