条形图上的值跨越了网格

条形图上的值跨越了网格

我试图将值放在图表的网格内,但正如您从下面的输出中看到的那样,数据点的值跨越了边界(左侧和右侧)。

我的输出:

在此处输入图片描述

乳胶代码:

\documentclass[10pt,journal,compsoc,xcolor={dvipsnames}]{IEEEtran}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
%\usepackage[dvipsnames]{xcolor}
\pgfplotsset{compat=1.18}
\usepackage{pgfplotstable}
\usepackage{graphicx}

\usepackage{multirow}
\usepackage{lipsum}

\begin{document}
\begin{figure*}
\begin{tikzpicture}
    \begin{groupplot}[
group style={group size=3 by 1}, 
    width=0.33\textwidth, height=1cm,
    xmajorgrids = true,
    tick align = outside, xtick pos = left,
    scale only axis,
    x post scale=0.85,
    enlarge y limits=0.15,
    ytick distance=1,
%   
    xmin=0,xmax=100,
    ytick = data, yticklabels = {N=3,N=6, N=12,N=24},
    xtick={0,20,...,100},
    xticklabel={\pgfmathprintnumber{\tick}\%},% <-- prints % sign after x tick value
    xticklabel style={font=\scriptsize},
    yticklabel style = {font=\scriptsize},
%    
    nodes near coords style={font=\scriptsize, 
                             yshift={ifthenelse(or(\plotnum == 5,\plotnum == 2),  8pt,8pt)}
                            }, % shift down the third and sixth node
%
    title style = {at={(0.5,-0.3)}, anchor=north},
    legend style={at={(0.5,1.05)}, anchor=south,
                  legend columns=4, font=\footnotesize,
                  cells={anchor=west},
                  /tikz/every even column/.append style={column sep=0.5em}
                  }
                    ]
\nextgroupplot[title=(a) MADDPG,
    xbar stacked,
    bar width=3mm,
    y=8mm,
    nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%},
                ]
\addplot [color=black, fill=teal] coordinates{(34.73,1) (19.64,2) (9.47,3) (18.45,4) };
\addplot [color=black, fill=yellow] coordinates{(16.57,1) (39.97,2) (56.37,3) (18.45,4)};
\addplot [color=black, fill=olive] coordinates{(36.56,1) (37.27,2)(32.63,3) (18.45,4)};
\addplot [color=black, fill=orange] coordinates{(12.14,1) (3.12,2) (1.53,3) (18.45,4)};

\nextgroupplot[title=(b) MATD3,
    xbar stacked,
    bar width=3mm,
    y=8mm,
    nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%},
                ]
\addplot [color=black, fill=teal] coordinates{(38,1) (22,2) (20.00,3) (18.45,4) };
\addplot [color=black, fill=yellow] coordinates{(22,1) (36,2) (20.00,3) (18.45,4)};
\addplot [color=black, fill=olive] coordinates{(37,1) (40,2)(20.00,3) (18.45,4)};
\addplot [color=black, fill=orange] coordinates{(3,1) (2,2) (20.00,3) (18.45,4)};

    %\legend{Action selection, Environment Interactions,   Gradient synchronization},
    \legend{Action selection,   Environment interactions, Update all trainers,
                    Other Parts},
\nextgroupplot[title=(c) MASAC,
    xbar stacked,
    bar width=3mm,
    y=8mm,
    nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%},
                ]
\addplot [color=black, fill=teal] coordinates{(43,1) (31,2) (20.00,3) (18.45,4) };
\addplot [color=black, fill=yellow] coordinates{(21,1) (36,2) (20.00,3) (18.45,4)};
\addplot [color=black, fill=olive] coordinates{(33,1) (31,2)(20.00,3) (18.45,4)};
\addplot [color=black, fill=orange] coordinates{(3,1) (2,2) (20.00,3) (18.45,4)};

    \end{groupplot}
\end{tikzpicture}

\label{figure1}
\end{figure*}
\end{document}

参考图

在此处输入图片描述

有人能帮我解决这个问题吗?

谢谢!

答案1

移除redcoordinate style/.condition={x-0.5*rawx>95}{red, xshift=-4pt},

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
group style={group size=3 by 1, horizontal sep=1.2cm}, 
xmajorgrids=true,
tick align=outside, xtick pos=left,
scale only axis,
x post scale=0.85,
enlarge y limits=0.18,
xmin=0, xmax=100,
xtick={0,20,...,100},
ytick=data, yticklabels={N=3,N=6, N=12,N=24},
xticklabel={\pgfmathprintnumber{\tick}\%},
xticklabel style={font=\scriptsize},
yticklabel style={font=\scriptsize, xshift=3pt},
nodes near coords style={font=\scriptsize,  yshift=8pt},
legend style={at={(0.5,1.05)}, anchor=south, legend columns=4, font=\footnotesize, cells={anchor=west}},
xbar stacked,
/pgf/bar width=3mm,
y=8mm,
title style={at={(0.5,-0.3)}, anchor=north},
nodes near coords={\pgfkeys{/pgf/number format/precision=0}\pgfmathprintnumber{\pgfplotspointmeta}\%},
%coordinate style/.condition={x-0.5*rawx<5}{red, xshift=4pt},
coordinate style/.condition={x-0.5*rawx>95}{red, xshift=-4pt},
]
\nextgroupplot[title=(a) MADDPG]
\addplot [color=black, fill=teal] coordinates{(34.73,1) (19.64,2) (9.47,3) (18.45,4) };
\addplot [color=black, fill=yellow] coordinates{(16.57,1) (39.97,2) (56.37,3) (18.45,4)};
\addplot [color=black, fill=olive] coordinates{(36.56,1) (37.27,2)(32.63,3) (18.45,4)};
\addplot [color=black, fill=orange] coordinates{(12.14,1) (3.12,2) (1.53,3) (18.45,4)};

\nextgroupplot[title=(b) MATD3]
\addplot [color=black, fill=teal] coordinates{(38,1) (22,2) (20.00,3) (18.45,4) };
\addplot [color=black, fill=yellow] coordinates{(22,1) (36,2) (20.00,3) (18.45,4)};
\addplot [color=black, fill=olive] coordinates{(37,1) (40,2)(20.00,3) (18.45,4)};
\addplot [color=black, fill=orange] coordinates{(3,1) (2,2) (20.00,3) (18.45,4)};
\legend{Action selection, Environment interactions, Update all trainers, Other Parts},

\nextgroupplot[title=(c) MASAC]
\addplot [color=black, fill=teal] coordinates{(43,1) (31,2) (20.00,3) (18.45,4) };
\addplot [color=black, fill=yellow] coordinates{(21,1) (36,2) (20.00,3) (18.45,4)};
\addplot [color=black, fill=olive] coordinates{(33,1) (31,2)(20.00,3) (18.45,4)};
\addplot [color=black, fill=orange] coordinates{(3,1) (2,2) (20.00,3) (18.45,4)};

\end{groupplot}
\end{tikzpicture}
\end{document}

三个 x 条形图

相关内容