我有一个带图例的条形图乳胶代码。我试图在侧面添加另外两个类似的图表,以便并排显示 3 个条形图,每个图表都有一个子标题,最终图表应该有一个标题。此外,我希望所有三个图表都有相同的图例,并且需要添加 x 轴和 y 轴的标签。
乳胶代码:
\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}
答案1
可以用该库创建图组groupplots
。
\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}