减少条形组之间的间隙并调整每个条形的条形值

减少条形组之间的间隙并调整每个条形的条形值

我试图减少条形之间的空间,也减少值和条形之间的空间。从下图可以看出,条形的值超出了图形边界。

我的输出:

在此处输入图片描述

乳胶代码:

\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.83,
    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),  13pt,13pt)}
                            }, % 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{(20.00,1) (18.45,2) (20.00,3) (18.45,4) };
\addplot [color=black, fill=yellow] coordinates{(28.57,1) (28.58,2) (20.00,3) (18.45,4)};
\addplot [color=black, fill=olive] coordinates{(11.43,1) (10.85,2)(20.00,3) (18.45,4)};
% \addplot [color=violet, fill=green] coordinates{(28.57,1) (28.01,2)};
% \addplot [color=violet, fill=orange] coordinates{(6.43,1) (7.55,2)};
\addplot [color=black, fill=orange] coordinates{(5.00,1) (6.56,2) (20.00,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{(20.00,1) (18.45,2) (20.00,3) (18.45,4) };
\addplot [color=black, fill=yellow] coordinates{(28.57,1) (28.58,2) (20.00,3) (18.45,4)};
\addplot [color=black, fill=olive] coordinates{(11.43,1) (10.85,2)(20.00,3) (18.45,4)};
% \addplot [color=violet, fill=green] coordinates{(28.57,1) (28.01,2)};
% \addplot [color=violet, fill=orange] coordinates{(6.43,1) (7.55,2)};
\addplot [color=black, fill=orange] coordinates{(5.00,1) (6.56,2) (20.00,3) (18.45,4)};

    %\legend{Action selection, Environment Interactions,   Gradient synchronization},
    \legend{Action selection,   Benchmark learned policies, Calculate rewards,
                    Collect experience, Environment Interactions,   Gradient synchronization},
\nextgroupplot[title=(c) MASAC,
    xbar stacked,
    bar width=3mm,
    y=8mm,
    nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%},
                ]
\addplot [color=black, fill=teal] coordinates{(20.00,1) (18.45,2) (20.00,3) (18.45,4) };
\addplot [color=black, fill=yellow] coordinates{(28.57,1) (28.58,2) (20.00,3) (18.45,4)};
\addplot [color=black, fill=olive] coordinates{(11.43,1) (10.85,2)(20.00,3) (18.45,4)};
% \addplot [color=violet, fill=green] coordinates{(28.57,1) (28.01,2)};
% \addplot [color=violet, fill=orange] coordinates{(6.43,1) (7.55,2)};
\addplot [color=black, fill=orange] coordinates{(5.00,1) (6.56,2) (20.00,3) (18.45,4)};

    \end{groupplot}
\end{tikzpicture}

\caption{Caption text}
\label{fig2}
\end{figure*}
\end{document}

重申一下,我希望每个条形的值更接近,并且当值超出网格时,是否有一些巧妙的解决方案?

谢谢!

答案1

对您的设置进行一些更改(已标记)来交替改变值的位置并将它们移近条形图。

nodes near coords style={yshift={ifthenelse(or(\plotnum == 3,\plotnum == 1), -7pt,7pt)},font=\tiny},

\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,
                ytick pos=left,%<<<<<<<<<<<<<<<<< added (supress right ticks)
                scale only axis,
                x post scale=.83,
                enlarge y limits=0.18,%<<<<<<<<<<<<<<<<<< changed
                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={
                    yshift={ifthenelse(or(\plotnum == 3,\plotnum == 1),  -7pt,7pt)},
                    font=\tiny,
                }, % shift down the second and fourth node <<<<<<<<<<<<<<<<<<< changed
                %
                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=7mm,%<<<<<<<<<<<<<<<<<
                nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%},
                ]
                \addplot [color=black, fill=teal] coordinates{(20.00,1) (18.45,2) (20.00,3) (18.45,4) };
                \addplot [color=black, fill=yellow] coordinates{(28.57,1) (28.58,2) (20.00,3) (18.45,4)};
                \addplot [color=black, fill=olive] coordinates{(11.43,1) (10.85,2)(20.00,3) (18.45,4)};
                % \addplot [color=violet, fill=green] coordinates{(28.57,1) (28.01,2)};
                % \addplot [color=violet, fill=orange] coordinates{(6.43,1) (7.55,2)};
                \addplot [color=black, fill=orange] coordinates{(5.00,1) (6.56,2) (20.00,3) (18.45,4)};             
            \end{groupplot}
        \end{tikzpicture}
        
        \caption{Caption text}
        \label{fig2}
    \end{figure*}
\end{document}

b

这是完整的代码,并添加了其他更改以使所有三个图表适合文本区域。

\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{showframe} % only to show the margins <<<<<<<<<<<

\begin{document}
    \begin{figure}
        \centering
        \begin{tikzpicture}         
            \begin{groupplot}[
                group style={group size=3 by 1}, 
                width=0.31\textwidth, height=1cm,%<<<<<<<<<<<<<<<<<<<<< changed to fit the text width
                xmajorgrids = true,
                tick align = outside, xtick pos = left,
                ytick pos=left,%<<<<<<<<<<<<<<<<< added (supress right ticks)
                scale only axis,
                x post scale=.83,
                enlarge y limits=0.18,%<<<<<<<<<<<<<<<<<< changed
                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={
                    yshift={ifthenelse(or(\plotnum == 1,\plotnum == 3),  -7pt,7pt)},
                    font=\tiny,
                }, % shift down the second and fourth node <<<<<<<<<<<<<<<<<<< changed
                %
                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=7mm,%<<<<<<<<<<<<<<<<<
                nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%},
                ]
                \addplot [color=black, fill=teal] coordinates{(20.00,1) (18.45,2) (20.00,3) (18.45,4) };
                \addplot [color=black, fill=yellow] coordinates{(28.57,1) (28.58,2) (20.00,3) (18.45,4)};
                \addplot [color=black, fill=olive] coordinates{(11.43,1) (10.85,2)(20.00,3) (18.45,4)};
                \addplot [color=black, fill=orange] coordinates{(5.00,1) (6.56,2) (20.00,3) (18.45,4)}; 

                \nextgroupplot[title=(b) MATD3,
                xbar stacked,
                bar width=3mm,
                y=7mm,%<<<<<<<<<<<<<<<<< changed
                xshift=8pt,%<<<<<<<<<<<<<<<<< added
                nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%},
                ]
                \addplot [color=black, fill=teal] coordinates{(20.00,1) (18.45,2) (20.00,3) (18.45,4) };
                \addplot [color=black, fill=yellow] coordinates{(28.57,1) (28.58,2) (20.00,3) (18.45,4)};
                \addplot [color=black, fill=olive] coordinates{(11.43,1) (10.85,2)(20.00,3) (18.45,4)};
                \addplot [color=black, fill=orange] coordinates{(5.00,1) (6.56,2) (20.00,3) (18.45,4)}; 
                
                \legend{Action selection,   Benchmark learned policies, Calculate rewards,
                        Collect experience, Environment Interactions,   Gradient synchronization};
                
                \nextgroupplot[title=(c) MASAC,
                xbar stacked,
                bar width=3mm,
                y=7mm,%<<<<<<<<<<<<<<<<<
                xshift=8pt,%<<<<<<<<<<<<<<<<< added
                nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%},
                ]
                \addplot [color=black, fill=teal] coordinates{(20.00,1) (18.45,2) (20.00,3) (18.45,4) };
                \addplot [color=black, fill=yellow] coordinates{(28.57,1) (28.58,2) (20.00,3) (18.45,4)};
                \addplot [color=black, fill=olive] coordinates{(11.43,1) (10.85,2)(20.00,3) (18.45,4)};
                \addplot [color=black, fill=orange] coordinates{(5.00,1) (6.56,2) (20.00,3) (18.45,4)}; 
            \end{groupplot}
        \end{tikzpicture}
        \caption{Caption text}
        \label{fig2}
    \end{figure}
\end{document}

C

相关内容