问题
我需要一个这样的图表(来自一篇研究论文):
我拥有的
目前我的输出如下所示:我尝试使用上面的参考图创建 3 个图表。但是,当我在 Overleaf 上用我的IEEEtran
文档执行此操作时,我发现图表超出了页面边界。如果您能举一个最简单的例子,我将不胜感激。
平均能量损失
\documentclass[10pt,journal,compsoc]{IEEEtran}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[group style={group size=3 by 3},width=10cm,height=25cm]
\nextgroupplot[
height=7cm,
width=\textwidth,
xmajorgrids = true,
xmin=0,xmax=100,
ytick = data, yticklabels = {1,2},
xtick={0,20,...,100},
tick align = outside, xtick pos = left,
x post scale=1.3,
scale only axis,
enlarge y limits=0.75,
xbar stacked,
bar width=6mm, y=14mm,
ytick=data,
nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%},
nodes near coords style={
yshift={
ifthenelse(or(\plotnum == 5,\plotnum == 2) , 13pt,13pt)
}
}, % shift down the third and sixth node
xticklabel={\pgfmathprintnumber{\tick}\%},% <-- prints % sign after x tick value
legend style={at={(-0.5,1.3)}, anchor=north,legend columns=-1,font=\normalsize},
]
\addplot coordinates{(20.00,1) (18.45,2)};
\addplot coordinates{(28.57,1) (28.58,2)};
\addplot coordinates{(11.43,1) (10.85,2)};
\addplot coordinates{(28.57,1) (28.01,2)};
\addplot coordinates{(6.43,1) (7.55,2)};
\addplot coordinates{(5.00,1) (6.56,2)};
%\legend{KNN,Neural Networks,Decision Tree,Random Forest,SVM,Naive Bayes}
\nextgroupplot[
height=7cm,
width=\textwidth,
xmajorgrids = true,
xmin=0,xmax=100,
ytick = data, yticklabels = {1,2},
xtick={0,20,...,100},
tick align = outside, xtick pos = left,
x post scale=1.3,
scale only axis,
enlarge y limits=0.75,
xbar stacked,
bar width=6mm, y=14mm,
ytick=data,
nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%},
nodes near coords style={
yshift={
ifthenelse(or(\plotnum == 5,\plotnum == 2) , 13pt,13pt)
}
}, % shift down the third and sixth node
xticklabel={\pgfmathprintnumber{\tick}\%},% <-- prints % sign after x tick value
legend style={at={(-0.5,1.3)}, anchor=north,legend columns=-1,font=\normalsize},
]
\addplot coordinates{(20.00,1) (18.45,2)};
\addplot coordinates{(28.57,1) (28.58,2)};
\addplot coordinates{(11.43,1) (10.85,2)};
\addplot coordinates{(28.57,1) (28.01,2)};
\addplot coordinates{(6.43,1) (7.55,2)};
\addplot coordinates{(5.00,1) (6.56,2)};
\nextgroupplot[
height=7cm,
width=\textwidth,
xmajorgrids = true,
xmin=0,xmax=100,
ytick = data, yticklabels = {1,2},
xtick={0,20,...,100},
tick align = outside, xtick pos = left,
x post scale=1.3,
scale only axis,
enlarge y limits=0.75,
xbar stacked,
bar width=6mm, y=14mm,
ytick=data,
nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%},
nodes near coords style={
yshift={
ifthenelse(or(\plotnum == 5,\plotnum == 2) , 13pt,13pt)
}
}, % shift down the third and sixth node
xticklabel={\pgfmathprintnumber{\tick}\%},% <-- prints % sign after x tick value
legend style={at={(-0.5,1.3)}, anchor=north,legend columns=-1,font=\normalsize},
]
\addplot coordinates{(20.00,1) (18.45,2)};
\addplot coordinates{(28.57,1) (28.58,2)};
\addplot coordinates{(11.43,1) (10.85,2)};
\addplot coordinates{(28.57,1) (28.01,2)};
\addplot coordinates{(6.43,1) (7.55,2)};
\addplot coordinates{(5.00,1) (6.56,2)};
\legend{Action selection, Benchmark learned policies,Calculate rewards,Collect experience,Environment Interactions, Gradient synchronization}
\end{groupplot}
\end{tikzpicture}
\end{document}
答案1
- 对于每个图表的宽度您都规定了
\textwidth
,因此只有第一个图表可见,第二个和第三个图表被推到页面之外。 - 为了使所有图表可见,请将其宽度设置为
0.3\textwidth
。 - 图像应插入
figure*
浮动中。 - 尚不清楚图表标题是否必须可引用。到目前为止,我认为不是,因此它们被添加为图表标题。
一些主题建议:
- 图像样式规范移至
groupplot
选项。 - 减小字体大小
nodes near coord
,xticklabel
并在legend
- 图例分三栏书写
\documentclass[10pt,journal,compsoc]{IEEEtran}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{figure*}
\begin{tikzpicture}
\begin{groupplot}[
group style={group size=3 by 3},
width=0.33\textwidth, height=7cm,
xmajorgrids = true,
tick align = outside, xtick pos = left,
scale only axis,
x post scale=0.85,
enlarge y limits=0.6,
ytick=data,
%
xmin=0,xmax=100,
ytick = data, yticklabels = {1,2},
xtick={0,20,...,100},
xticklabel={\pgfmathprintnumber{\tick}\%},% <-- prints % sign after x tick value
xticklabel style={font=\small},
%
nodes near coords style={font=\scriptsize,
yshift={ifthenelse(or(\plotnum == 2,\plotnum == 4), -14pt, 14pt)}
}, % 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=3, font=\small,
cells={anchor=west},
/tikz/every even column/.append style={column sep=1em}
}
]
\nextgroupplot[title=(a) title,
xbar stacked,
bar width=5mm,
y=14mm,
nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%},
]
\addplot coordinates{(20.00,1) (18.45,2)};
\addplot coordinates{(28.57,1) (28.58,2)};
\addplot coordinates{(11.43,1) (10.85,2)};
\addplot coordinates{(28.57,1) (28.01,2)};
\addplot coordinates{(6.43,1) (7.55,2)};
\addplot coordinates{(5.00,1) (6.56,2)};
\nextgroupplot[title=(b) title,
xbar stacked,
bar width=5mm,
y=14mm,
nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%},
]
\addplot coordinates{(20.00,1) (18.45,2)};
\addplot coordinates{(28.57,1) (28.58,2)};
\addplot coordinates{(11.43,1) (10.85,2)};
\addplot coordinates{(28.57,1) (28.01,2)};
\addplot coordinates{(6.43,1) (7.55,2)};
\addplot coordinates{(5.00,1) (6.56,2)};
\legend{Action selection, Benchmark learned policies, Calculate rewards,
Collect experience, Environment Interactions, Gradient synchronization},
\nextgroupplot[title=(c) title,
xbar stacked,
bar width=5mm,
y=14mm,
nodes near coords={\pgfmathprintnumber{\pgfplotspointmeta}\%},
]
\addplot coordinates{(20.00,1) (18.45,2)};
\addplot coordinates{(28.57,1) (28.58,2)};
\addplot coordinates{(11.43,1) (10.85,2)};
\addplot coordinates{(28.57,1) (28.01,2)};
\addplot coordinates{(6.43,1) (7.55,2)};
\addplot coordinates{(5.00,1) (6.56,2)};
\end{groupplot}
\end{tikzpicture}
\caption{Caption text}
\label{fig:?}
\end{figure*}
\end{document}
(红线表示文本块边框)