堆积条形图中的小值无法正常显示

堆积条形图中的小值无法正常显示

问题: 我希望 (a) 和 (b) 条形图中 N=48 的小值 (5% 和 6%) 能够正确显示,而不是显示在网格上。我尝试更改限制,增加尺寸,但不幸的是没有任何效果。有人能帮我解决这个问题吗?

输出 在此处输入图片描述

乳胶代码:

\documentclass[10pt,journal,compsoc,xcolor={dvipsnames}]{IEEEtran}

\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.18}
\usepackage{pgfplotstable}
\usepackage{graphicx}
\usepackage{subfig}
\usepackage{multirow}
\usepackage{lipsum}
\usepackage{xspace}
\usetikzlibrary{patterns}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\newcommand{\R}{\mathbb{R}}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
            \node[shape=circle,draw,inner sep=0.7pt] (char) {#1};}}

\begin{document}




\begin{figure*}[h]
\begin{tikzpicture}
\begin{groupplot}[
group style={group size=3 by 1, horizontal sep=1.2cm}, 
width=0.3\textwidth, height=0.3cm,
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,N=48},
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=2.5mm,
y=5.2mm,
legend image code/.code={%
            \draw[#1] (0cm,-0.1cm) rectangle (0.6cm,0.2cm);
            },
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}{xshift=-4pt},
]

\nextgroupplot[title=(a) MADDPG]
\addplot [color=black, pattern color=black, pattern=crosshatch dots] coordinates{(49.93,1) (34.73,2) (22.72,3) (12,4) (6,5)};
%\addplot [color=black,  pattern color=gray, pattern=checkerboard] 
%coordinates{(16.57,1) (39.97,2) (56.37,3) (0,4)};
\addplot [color=black,  pattern color=gray, pattern=checkerboard] coordinates{(47.55,1) (63.10,2)(75.80,3) (88,4) (94,5)};
\addplot [color=black,  pattern color=black, pattern=north west lines] coordinates{(2.04,1) (1.64,2) (1.33,3) (0,4) (0,5)};
%4
\nextgroupplot[title=(b) MATD3]
\addplot [color=black,  pattern color=black, pattern=crosshatch dots] 
coordinates{(49,1) (35,2) (21,3) (10,4) (5,5)};
% \addplot [color=black, pattern color=gray, pattern=checkerboard] 
% coordinates{(22,1) (36,2) (50.12,3) (0,4)};
\addplot [color=black,  pattern color=gray, pattern=checkerboard] 
coordinates{(49,1) (64,2)(78,3) (89,4) (95,5)};
\addplot [color=black, pattern color=black, pattern=north west lines] 
coordinates{(2,1) (1,2) (1,3) (1,4) (0,5)};
\legend{Action selection, Update all trainers, Other Parts},
%3,4

\nextgroupplot[title=(c) MASAC]
\addplot [color=black,  pattern color=black, pattern=crosshatch dots]
coordinates{(55,1) (49,2) (42,3) (26,4) (14,5)};
% \addplot [color=black, pattern color=gray, pattern=checkerboard] 
% coordinates{(21,1) (36,2) (51,3) (0,4)};
\addplot [color=black,   pattern color=gray, pattern=checkerboard] coordinates{(43,1) (49,2)(58,3) (74,4) (86,5)};
\addplot [color=black, pattern color=black, pattern=north west lines] 
coordinates{(2,1) (2,2) (0,3) (0,4) (0,5)};
% %3,4

\end{groupplot}
\end{tikzpicture}
\vspace{-\baselineskip}
\caption{Training time breakdown for three MARL workloads with 3, 6, 12 \& 24 agents. The environment is Cooperative Navigation~(simple spread).}
\label{figure1}
\end{figure*}
\end{document}

答案1

您可以根据 的值添加一些水平空间\pgfplotspointmeta。此宏存储在低级浮点数中,不直接适用于数字比较,但 pgfplots 还提供了\pgfplotspointmetatransformed定点表示,其缩放比例在 0 到 1000 之间。在这种情况下,您可以检查值是否小于 10%,例如,在缩放版本中,这意味着您检查值是否 <100。在这种情况下,您可以在打印值之前添加例如 4pt:

nodes near coords={\pgfmathparse{less(\pgfplotspointmetatransformed,100)}%
\ifnum\pgfmathresult=1%
\hspace{4pt}%
\fi%
\pgfkeys{/pgf/number format/precision=0}\pgfmathprintnumber{\pgfplotspointmeta}\%}

当然,这应该只发生在条形图的左侧,否则右侧的 1% 和 2% 值将被推到图外。为此,您可以只对有问题的线添加检查\addplot,这将覆盖默认规范。

\begin{groupplot}[
group style={group size=3 by 1, horizontal sep=1.2cm}, 
width=0.3\textwidth, height=0.3cm,
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,N=48},
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=2.5mm,
y=5.2mm,
legend image code/.code={%
            \draw[#1] (0cm,-0.1cm) rectangle (0.6cm,0.2cm);
            },
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}{xshift=-4pt},
]

\nextgroupplot[title=(a) MADDPG]
\addplot [color=black, pattern color=black, pattern=crosshatch dots, %
nodes near coords={\pgfmathparse{less(\pgfplotspointmetatransformed,100)}%
\ifnum\pgfmathresult=1%
\hspace{4pt}%
\fi%
\pgfkeys{/pgf/number format/precision=0}\pgfmathprintnumber{\pgfplotspointmeta}\%}]
coordinates{(49.93,1) (34.73,2) (22.72,3) (12,4) (6,5)};
%\addplot [color=black,  pattern color=gray, pattern=checkerboard] 
%coordinates{(16.57,1) (39.97,2) (56.37,3) (0,4)};
\addplot [color=black,  pattern color=gray, pattern=checkerboard] coordinates{(47.55,1) (63.10,2)(75.80,3) (88,4) (94,5)};
\addplot [color=black,  pattern color=black, pattern=north west lines] coordinates{(2.04,1) (1.64,2) (1.33,3) (0,4) (0,5)};
%4
\nextgroupplot[title=(b) MATD3]
\addplot [color=black,  pattern color=black, pattern=crosshatch dots, %
nodes near coords={\pgfmathparse{less(\pgfplotspointmetatransformed,100)}%
\ifnum\pgfmathresult=1%
\hspace{4pt}%
\fi%
\pgfkeys{/pgf/number format/precision=0}\pgfmathprintnumber{\pgfplotspointmeta}\%}]
coordinates{(49,1) (35,2) (21,3) (10,4) (5,5)};
% \addplot [color=black, pattern color=gray, pattern=checkerboard] 
% coordinates{(22,1) (36,2) (50.12,3) (0,4)};
\addplot [color=black,  pattern color=gray, pattern=checkerboard] 
coordinates{(49,1) (64,2)(78,3) (89,4) (95,5)};
\addplot [color=black, pattern color=black, pattern=north west lines] 
coordinates{(2,1) (1,2) (1,3) (1,4) (0,5)};
\legend{Action selection, Update all trainers, Other Parts},
%3,4

\nextgroupplot[title=(c) MASAC]
\addplot [color=black,  pattern color=black, pattern=crosshatch dots]
coordinates{(55,1) (49,2) (42,3) (26,4) (14,5)};
% \addplot [color=black, pattern color=gray, pattern=checkerboard] 
% coordinates{(21,1) (36,2) (51,3) (0,4)};
\addplot [color=black,   pattern color=gray, pattern=checkerboard] coordinates{(43,1) (49,2)(58,3) (74,4) (86,5)};
\addplot [color=black, pattern color=black, pattern=north west lines] 
coordinates{(2,1) (2,2) (0,3) (0,4) (0,5)};
% %3,4

\end{groupplot}

结果:

在此处输入图片描述

相关内容