2

I have a latex code for bar chart with a legend. I'm trying to add two more similar charts on the side to have 3 bar charts side by side with each chart having a sub-caption and the final figure should have a caption. Also, I want a same legend for all the three charts and need to add the labels for x and y axis.

Latex code:

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{
    selective show sum on top/.style={
        /pgfplots/scatter/@post marker code/.append code={%
            \ifnum\coordindex=#1
               \node[
               at={(normalized axis cs:%
                   \pgfkeysvalueof{/data point/x},%
                   \pgfkeysvalueof{/data point/y})%
               },
               anchor=south,
               ]
               {\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}};
            \fi
        },
    },selective show sum on top/.default=0
}

\begin{axis}[width=8cm,
ybar stacked, ymin=0,  
bar width=8mm,
symbolic x coords={3,6,12,24,48},
xtick=data,
yticklabel={\pgfmathprintnumber{\tick}\%},
nodes near coords={\pgfkeys{/pgf/number format/precision=0}\pgfmathprintnumber{\pgfplotspointmeta}\%},
nodes near coords style={font=\scriptsize,  yshift=5.5pt},
font=\footnotesize,
legend style={at={(0.05,1.1)}, anchor=west, legend columns=2}, 
]

\addplot [fill=black!20] coordinates {
    ({3},62)
    ({6},50)
    ({12},36)
    ({24},22)
    ({48},12)};
\addplot [fill=olive!20] coordinates {
   ({3},34)
    ({6},46)
    ({12},61)
    ({24},76)
    ({48},87)};
\addplot [fill=brown!80] coordinates {
    ({3},4)
    ({6},4)
    ({12},3)
    ({24},2)
    ({48},1)};
\legend{Action Selection, Update all trainers, Other segments}
\end{axis}

\end{tikzpicture} \end{document}

My output: enter image description here

Required output: enter image description here

Student
  • 197

1 Answers1

1

Groups of plots can be created with the groupplots library.

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{tikzpicture}
\pgfplotsset{
    selective show sum on top/.style={
        /pgfplots/scatter/@post marker code/.append code={%
            \ifnum\coordindex=#1
               \node[
               at={(normalized axis cs:%
                   \pgfkeysvalueof{/data point/x},%
                   \pgfkeysvalueof{/data point/y})%
               },
               anchor=south,
               ]
               {\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}};
            \fi
        },
    },selective show sum on top/.default=0
}

\begin{groupplot}[group style={group size=3 by 1},
    /pgfplots/legend style={at={(-0.75,1.1)}, anchor=south, legend columns=3}]
    \pgfplotsset{my bar/.style={width=6cm,
        ybar stacked, ymin=0,  
        bar width=3mm,
        symbolic x coords={3,6,12,24,48},
        xtick=data,
        yticklabel={\pgfmathprintnumber{\tick}\%},
        nodes near coords={\pgfkeys{/pgf/number format/precision=0}\pgfmathprintnumber{\pgfplotspointmeta}\%},
        nodes near coords style={font=\scriptsize,  yshift=5.5pt},
        font=\footnotesize,
        }}
    \nextgroupplot[my bar]
        \addplot [fill=black!20] coordinates {
            ({3},62)
            ({6},50)
            ({12},36)
            ({24},22)
            ({48},12)};
        \addplot [fill=olive!20] coordinates {
            ({3},34)
            ({6},46)
            ({12},61)
            ({24},76)
            ({48},87)};
        \addplot [fill=brown!80] coordinates {
            ({3},4)
            ({6},4)
            ({12},3)
            ({24},2)
            ({48},1)};
            \nextgroupplot[my bar]
            \addplot [fill=black!20] coordinates {
                ({3},62)
                ({6},50)
                ({12},36)
                ({24},22)
                ({48},12)};
            \addplot [fill=olive!20] coordinates {
                ({3},34)
                ({6},46)
                ({12},61)
                ({24},76)
                ({48},87)};
            \addplot [fill=brown!80] coordinates {
                ({3},4)
                ({6},4)
                ({12},3)
                ({24},2)
                ({48},1)};
            \nextgroupplot[my bar]
                \addplot [fill=black!20] coordinates {
                    ({3},62)
                    ({6},50)
                    ({12},36)
                    ({24},22)
                    ({48},12)};
                \addplot [fill=olive!20] coordinates {
                    ({3},34)
                    ({6},46)
                    ({12},61)
                    ({24},76)
                    ({48},87)};
                \addplot [fill=brown!80] coordinates {
                    ({3},4)
                    ({6},4)
                    ({12},3)
                    ({24},2)
                    ({48},1)};                    
\legend{Action Selection, Update all trainers, Other segments}
\end{groupplot}

\end{tikzpicture} \end{document}

enter image description here