막대 차트 그룹의 마지막 차트에는 두 개의 열이 있어야 합니다.

막대 차트 그룹의 마지막 차트에는 두 개의 열이 있어야 합니다.

나는 3개의 숫자로 막대 차트 그룹을 생성하려고 시도하고 있으며 성공적으로 완료했으며 출력은 아래에서 볼 수 있습니다.

문제: 동일한 상태의 처음 두 개의 막대 차트(a 및 b)를 원하지만 세 번째 막대 차트((c) Q 손실 및 P 손실)의 경우 X축의 각 레이블에 대해 두 개의 막대를 어떻게 포함할 수 있습니까? 그 차트에 범례만 있는 건가요? 다시 한번 말씀드리지만, 3-6에 대한 막대 2개, 6-12에 대한 막대 2개, 12-24에 대한 막대 2개와 세 번째 차트 전용 범례가 필요합니다. 또한 누군가 막대 차트(a,b,c) 사이의 공간을 줄이는 데 도움을 줄 수 있습니까?

산출:

여기에 이미지 설명을 입력하세요

라텍스 코드:

\documentclass[10pt,journal,compsoc,xcolor={dvipsnames}]{IEEEtran}
%[tikz]{standalone}
% If IEEEtran.cls has not been installed into the LaTeX system files,
% manually specify the path to it like:
% \documentclass[10pt,journal,compsoc]{../sty/IEEEtran}

%\usepackage{showframe}
%\renewcommand\ShowFrameLinethickness{0.15pt}
%\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
%\usepackage[dvipsnames]{xcolor}
\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}[!htpb]
        \centering
        \begin{tikzpicture}
            \begin{groupplot}[
                    legend columns=-1,
                    %bar width=0.22, % Width of the bar
                    %, % Distance between the centers of the bars
                    symbolic x coords={3-6, 6-12, 12-24},
                     enlarge x limits={abs=0.4cm},
                     %width=0.25\textwidth,
                    xtick={3-6, 6-12, 12-24},
                    %height=0.6\textheight,
                    ymin=0,
                    ymax=7,
                    xticklabel style={font=\tiny},
                    nodes near coords,
    every node near coord/.append style={font=\tiny},
   nodes near coords align={vertical},
                     xlabel={\tiny Agents~$(N \to \ 2N)$},
                     ylabel={\tiny Growth rate~($N\times$)},
                     % The distance between the center of the first bar and the left edge
                    %enlarge y limits=false
                    legend entries={{\color{olive}{\tiny Random}},{\color{orange}{\tiny ++Cost}},{\color{teal}{\tiny ++FTE}}},
                    legend to name=CombinedLegendBar,
                    footnotesize,
                    ybar legend,
                    width=0.4\columnwidth,
                    legend image code/.code={%
                     \draw[#1] (0cm,-0.1cm) rectangle (0.6cm,0.1cm);
                         },
                    % area legend, % This is the alternate option
                    group style={
                    group size=3 by 1,
                    xlabels at=edge bottom,
                    ylabels at=edge left}]
                \nextgroupplot[title={\tiny (a) Mini-batch sampling}]
                    \addplot[ybar, pattern color=black, pattern=crosshatch dots] coordinates {  (3-6, 3.5)};
                    \addplot[ybar, pattern color=gray, pattern=checkerboard] coordinates { (6-12, 4.1)};
                    \addplot[ybar, pattern color=gray, pattern=north east lines] coordinates {  (12-24, 4.5)};
                \nextgroupplot[title={\tiny (b) Target Q Calculation}]
                 \addplot[ybar, pattern color=black, pattern=crosshatch dots] coordinates {  (3-6, 4.1)};
                    \addplot[ybar, pattern color=gray, pattern=checkerboard] coordinates { (6-12, 4.6)};
                    \addplot[ybar,pattern color=gray, pattern=north east lines] coordinates {  (12-24, 4.6)};
                \nextgroupplot[title={\tiny (c) Q loss \& P loss}]
            `   \addplot[ybar, pattern color=black, pattern=crosshatch dots] coordinates {  (3-6, 3.05)};
                    \addplot[ybar, pattern color=gray, pattern=checkerboard] coordinates { (6-12,3.23)};
                    \addplot[ybar, pattern color=gray, pattern=north east lines] coordinates {  (12-24, 3.42)};
            \end{groupplot}
        \end{tikzpicture}
\vspace{-\baselineskip}
\caption{Update all trainers sub-functions super-linear growth rate for all the three workloads when the number of agents are scaled from $N-2N$.}% The environment is Cooperative Navigation (simple spread).}
\vspace{-\baselineskip}
\label{Figure4}
\end{figure}
\end{document}

누구든지 이 문제를 해결하도록 도와줄 수 있나요?

감사해요!

답변1

  • 분명히 질문에 대답하려는 첫 번째 시도에서 나는 그것을 오해했습니다 ...
  • 멋진 @Torbjørn T. 답변에서 배웠습니다. 이제 주요 문제는 마지막 다이어그램 P에 파란색 막대 세트를 추가하는 것입니다.
  • 이 작업은 상대적으로 쉬울 수 있습니다.
    • 막대의 Q 세트(검은색)가 왼쪽으로 조금씩 이동합니다.
    • 새로운 막대 P 세트(파란색)를 추가하고 같은 양만큼 오른쪽으로 이동합니다.
  • 새로운 MWE에서는 첫 번째 답변 정의 버전의 다이어그램 스타일이 보존됩니다.
  • 패턴의 경우 이제 patterns.meta라이브러리가 사용됩니다.
\documentclass[journal,compsoc,xcolor={dvipsnames}]{IEEEtran}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{patterns, patterns.meta}

\begin{document}
    \begin{figure}[ht]
    \raggedright
\begin{tikzpicture}[
pattern-A/.style = {pattern={Dots[angle=45, distance=2pt,xshift=1pt]}},
pattern-B/.style = {pattern={Hatch[angle=45,distance=2pt,xshift=1pt]}},
pattern-C/.style = {pattern={Lines[angle=45,distance=2pt]}},
%
pattern-AA/.style = {pattern-A, pattern color=blue},
pattern-BB/.style = {pattern-B, pattern color=blue},
pattern-CC/.style = {pattern-C, pattern color=blue},
                    ]
    \begin{groupplot}[
group style={
    group size=3 by 1,
    ylabels at=edge left,
    horizontal sep=4mm,
            },
    width=0.44\linewidth,
    enlarge x limits=0.4,
%
    title style = {yshift=-1ex, font=\scriptsize, align=center},
    xlabel={Agents~$(N \to \ 2N)$},
    ylabel={Growth rate~($N\times$)},
    label style = {font=\scriptsize},
%
    ybar,                   % <---
    /pgf/bar width=2.4mm,   % <--- common set, overwritten in third \nextgroupplot
    /pgf/bar shift=0pt,     % <--- common set, overwritten in third \nextgroupplot
%
    symbolic x coords={3-6, 6-12, 12-24},
    ticklabel style={font=\scriptsize},
    ymin=0,    ymax=7,
%
    nodes near coords,
    every node near coord/.append style={font=\tiny},
    nodes near coords align={vertical},
%
    legend image code/.code={\draw[#1] (0cm,-1mm) rectangle (4mm,1mm);},
    legend style={legend columns=-1,
                  font=\footnotesize,
                  /tikz/every even column/.append style={column sep=1em}
                  }
                    ]
\nextgroupplot[title={(a) Mini-batch\\ sampling}]
    \addplot[pattern-A] coordinates { (3-6, 3.5)};
    \addplot[pattern-B] coordinates { (6-12, 4.1)};
    \addplot[pattern-C] coordinates { (12-24, 4.5)};
\nextgroupplot[title={(b) Target Q\\ Calculation}]
    \addplot[pattern-A] coordinates { (3-6, 4.1)};
    \addplot[pattern-B] coordinates { (6-12, 4.6)};
    \addplot[pattern-C] coordinates { (12-24, 4.6)};

\nextgroupplot[title={(c) Q loss\\ \& \textcolor{blue}{P loss}},
               nodes near coords style={rotate=90, anchor=west},
               legend to name=CombinedLegendBar,                
               ]
    \addplot[pattern-A,bar shift=-1.4mm] coordinates { (3-6,  3.05)};
    \addplot[pattern-B,bar shift=-1.4mm] coordinates { (6-12, 3.23)};
    \addplot[pattern-C,bar shift=-1.4mm] coordinates { (12-24,3.42)};
    %
    \addplot[pattern-AA,bar shift=+1.4mm] coordinates { (3-6,4.05)};
    \addplot[pattern-BB,bar shift=+1.4mm] coordinates { (6-12,4.23)};
    \addplot[pattern-CC,bar shift=+1.4mm] coordinates { (12-24,4.42)};
\legend{Qa, Qb, Qc, Pa, Pb, Pc}
    
\end{groupplot}
\node [above=2\baselineskip] at (group c2r1.north) {\ref{CombinedLegendBar}};
        \end{tikzpicture}

\caption{Update all trainers sub-functions super-linear growth rate for all the three workloads when the number of agents are scaled from $N-2N$.}% The environment is Cooperative Navigation (simple spread).}
\label{Figure4}
\end{figure}
\end{document}

여기에 이미지 설명을 입력하세요

답변2

약간의 추가 조정이 필요할 수 있으므로 자리 표시자 항목을 사용하여 플롯 위에 범례를 표시했습니다. 실제로 축 내부에는 범례를 위한 공간이 없습니다.

주요 변경 사항은 각각 대신 ybar에 옵션을 사용 하고 수동 막대 이동을 사용하여 막대를 적절하게 배치하는 것입니다.groupplot\addplot

horizontal sep세트 에서는 group style축 사이의 공간이 설정됩니다.

여기에 이미지 설명을 입력하세요

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

\usepackage{pgfplotstable}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{patterns}
\begin{document}

\begin{figure}[!htpb]
        \centering
        \begin{tikzpicture}
            \begin{groupplot}[
                    legend columns=-1,
                    %bar width=0.22, % Width of the bar
                    %, % Distance between the centers of the bars
                    symbolic x coords={3-6, 6-12, 12-24},
                    enlarge x limits=0.3,
                     %width=0.25\textwidth,
                    xtick={3-6, 6-12, 12-24},
                    %height=0.6\textheight,
                    ymin=0,
                    ymax=7,
                    xticklabel style={font=\tiny},
                    nodes near coords,
                    every node near coord/.append style={font=\tiny},
                    nodes near coords align={vertical},
                    xlabel={\tiny Agents~$(N \to \ 2N)$},
                    ylabel={\tiny Growth rate~($N\times$)},
                     % The distance between the center of the first bar and the left edge
                    %enlarge y limits=false 
                    footnotesize,
                    ybar=-\pgfplotbarwidth,
                    width=0.45\columnwidth,
                    legend image code/.code={%
                     \draw[#1] (0cm,-0.1cm) rectangle (0.2cm,0.2cm);
                         },
                    legend style={cells={font=\scriptsize}},
                    % area legend, % This is the alternate option
                    group style={
                    group name=foo,
                    group size=3 by 1,
                    xlabels at=edge bottom,
                    ylabels at=edge left,
                    horizontal sep=4.5mm
                    },
                    title style={font=\tiny}]
                \nextgroupplot[title={(a) Mini-batch sampling}]
                    \addplot[pattern color=black, pattern=crosshatch dots] coordinates {  (3-6, 3.5)};
                    \addplot[pattern color=gray, pattern=checkerboard] coordinates { (6-12, 4.1)};
                    \addplot[pattern color=gray, pattern=north east lines] coordinates {  (12-24, 4.5)};
                \nextgroupplot[title={(b) Target Q Calculation}]
                 \addplot[pattern color=black, pattern=crosshatch dots] coordinates {  (3-6, 4.1)};
                    \addplot[pattern color=gray, pattern=checkerboard] coordinates { (6-12, 4.6)};
                    \addplot[ybar,pattern color=gray, pattern=north east lines] coordinates {  (12-24, 4.6)};


                \nextgroupplot[
                    title={(c) Q loss \& P loss},
                    bar width=8pt,
                    legend to name=CombinedLegendBar,
]
                
                \pgfmathsetlengthmacro{\MyBarshift}{0.5*\pgfplotbarwidth+1pt}

               \addplot[bar shift=-\MyBarshift,pattern color=black, pattern=crosshatch dots] coordinates {  (3-6, 3.05)};
               \addplot[bar shift=-\MyBarshift,pattern color=gray, pattern=checkerboard] coordinates { (6-12,3.23)};
               \addplot[bar shift=-\MyBarshift,pattern color=gray, pattern=north east lines] coordinates {  (12-24, 3.42)};
                    
                    
               % second set of bars
               \addplot[bar shift=\MyBarshift,pattern color=blue, pattern=crosshatch dots] coordinates {  (3-6, 4.05)};
               \addplot[bar shift=\MyBarshift,pattern color=blue, pattern=checkerboard] coordinates { (6-12,4.23)};
               \addplot[bar shift=\MyBarshift,pattern color=blue, pattern=north east lines] coordinates {  (12-24, 4.42)};
               
               \legend{Qa,Qb,Qc,Pa,Pb,Pc}

            \end{groupplot}
            
            \node [above=12pt] at (foo c2r1.north) {\ref{CombinedLegendBar}};
        \end{tikzpicture}
\vspace{-\baselineskip}
\caption{Update all trainers sub-functions super-linear growth rate for all the three workloads when the number of agents are scaled from $N-2N$.}% The environment is Cooperative Navigation (simple spread).}
\vspace{-\baselineskip}
\label{Figure4}
\end{figure}
\end{document}

답변3

여기에 이미지 설명을 입력하세요

  • 막대는 pic세 가지 인수(높이, 레이블 및 채우기 패턴)에 따라 달라지는 요소를 통해 생성됩니다.

  • 치수는 처음에 전역 변수로 제어됩니다. 원하는 대로 수정할 수 있습니다.

코드

\documentclass[11pt, margin=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{math, patterns}
\begin{document}

\tikzmath{
  real \w, \dw, \globalW, \globalH, \g1, \g2, \g3;
  \w = 1.5;  % width of a bar
  \dw = .4;  % half inbetween two bars
  \globalW = 3*\w +6*\dw;
  \globalH = 6;
  \g1 = 0;  % position of the first graph
  \g2 = \globalW +1.3*\w;
  \g3 = 2*\globalW +4*\w;
}
\tikzset{
  pics/bar/.style n args={3}{%  height, label, pattern 
    code={%
      \draw[pattern=#3] (0, 0) rectangle (\w, #1/7*\globalH)
      ++ (-\w/2, 1ex) node[above, scale=.8] {#1};
      \path (\w/2, -1ex) node[below, scale=.8] {#2};
    }
  }
}
\begin{tikzpicture}[every node/.style={scale=.9}]
  \begin{scope}[xshift={\g1 cm}]
    \path (\globalW/2, \globalH) node[above] {(a) Mini-batch sampling};
    \draw (0, 0) -- ++(\globalW, 0) ++(-\globalW/2, -4ex)
    node[below] {Agents $(N \to 2N)$};
    \draw (0, 0) -- ++(0, \globalH) ++(-3em, -\globalH/2)
    node[rotate=90] {Growth rate $(N\times)$};
    \foreach \j in {0, 2, 4, 6}{%
      \draw (0, \j/7*\globalH) -- ++(-1ex, 0) node[left] {$\j$};
    }
    \path
    (0, 0) ++(\dw, 0) pic {bar={3.5}{3-6}{dots}}
    ++(\w +2*\dw, 0) pic {bar={4.1}{6-12}{crosshatch}}
    ++(\w +2*\dw, 0) pic {bar={4.5}{12-24}{north east lines}};
  \end{scope}
  \begin{scope}[xshift={\g2 cm}]
    \path (\globalW/2, \globalH) node[above] {(b) Target Q Calculation};
    \draw (0, 0) -- ++(\globalW, 0) ++(-\globalW/2, -4ex)
    node[below] {Agents $(N \to 2N)$};
    \draw (0, 0) -- ++(0, \globalH) ++(-3em, -\globalH/2);
    \foreach \j in {0, 2, 4, 6}{%
      \draw (0, \j/7*\globalH) -- ++(-1ex, 0) node[left] {$\j$};
    }
    \path
    (0, 0) ++(\dw, 0) pic {bar={4.1}{3-6}{dots}}
    ++(\w +2*\dw, 0) pic {bar={4.6}{6-12}{crosshatch}}
    ++(\w +2*\dw, 0) pic {bar={4.6}{12-24}{north east lines}};
  \end{scope}
  \begin{scope}[xshift={\g3 cm}]
    \path (\globalW/2, \globalH) node[above] {(c) Q loss \& P loss};
    \draw (0, 0) -- ++(\globalW, 0) ++(-\globalW/2, -4ex)
    node[below] {Agents $(N \to 2N)$};
    \draw (0, 0) -- ++(0, \globalH) ++(-3em, -\globalH/2)
    node[rotate=90] {Growth rate $(N\times)$};
    \foreach \j in {0, 2, 4, 6}{%
      \draw (0, \j/7*\globalH) -- ++(-1ex, 0) node[left] {$\j$};
    }
    \path
    (0, 0) ++(\dw, 0) pic {bar={3.05}{3-6}{dots}}
    ++(\w +2*\dw, 0) pic {bar={3.25}{6-12}{crosshatch}}
    ++(\w +2*\dw, 0) pic {bar={3.42}{12-24}{north east lines}};
  \end{scope}
\end{tikzpicture}
\end{document}

관련 정보