我正在使用 LaTeX 模板,即 Elsivier 的 elsarticle 类,其中我使用 subcaption 包创建了两个子图。我遇到了这个答案它提供了一种为子图提供通用图例的解决方案,但它使用了 subfigure 包。不幸的是,我已经在草稿中为之前的图使用了 subcaption 包。
当我尝试将提供的代码集成到我的文档中时,遇到了如图所示的错误。
我怀疑这些错误可能源于 subfigure 和 subcaption 包之间的冲突。
我的主要问题是:在使用子标题包时,如何修改代码以使两个子图具有单个共同的图例?
以下是生成图形的代码片段:
\documentclass[preprint,3p,number]{elsarticle}
\usepackage{amssymb}
\usepackage[breaklinks=true,colorlinks,bookmarks=true]{hyperref}
\usepackage[inline]{enumitem}
\usepackage{times}
\usepackage{comment}
\usepackage{epsfig}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{enumitem}
\setitemize{noitemsep,topsep=0pt,parsep=0pt,partopsep=0pt}
\usepackage{subcaption}
\begin{document}
\begin{frontmatter}
%% Title, authors and addresses
\title{2 subfigures with 1 common legend}
\begin{abstract}
\end{abstract}
\begin{keyword}
% keywords here, in the form: keyword \sep keyword
\end{keyword}
\end{frontmatter}
%% main text
\begin{figure}[ht!]
\begin{subfigure}{0.48\textwidth}
\begin{tikzpicture}
\begin{axis}[
ybar=0pt,
enlargelimits=0.15,
width=\columnwidth,
ymin=90,
ylabel={Accuracy (\%)},
symbolic x coords={VGG,GoogLeNet,DenseNet,ResNet},
xtick=data,
bar width=10pt,
legend style={
at={(0.5,-0.15)},
anchor=north,
legend columns=-1,
},
axis x line*=bottom,
axis y line*=left,
]
\addplot [blue, fill, opacity=0.7, nodes near coords,every node near coord/.append style={xshift=5pt,yshift=12pt,font=\footnotesize,rotate=90, opacity=1}]coordinates {(VGG, 93.86) (GoogLeNet, 95.22) (DenseNet, 94.85) (ResNet, 93.98)};
\addplot [green, fill, opacity=0.7, nodes near coords,every node near coord/.append style={xshift=5pt,yshift=12pt,font=\footnotesize,rotate=90, opacity=1}]coordinates {(VGG, 93.81) (GoogLeNet, 95.3) (DenseNet, 94.45) (ResNet, 93.97)};
\addplot [red, fill, opacity=0.7, nodes near coords,every node near coord/.append style={xshift=5pt,yshift=12pt,font=\footnotesize,rotate=90, opacity=1}]coordinates {(VGG, 93.86) (GoogLeNet, 95.09) (DenseNet, 94.53) (ResNet, 94.17)};
\legend{Cosine, Euclide, VBD}
\end{axis}
\end{tikzpicture}
\caption{CIFAR-10.}
\label{subfig:cifar10}
\end{subfigure}
%------------------------------------------------------------------------
\qquad
\begin{subfigure}{0.48\textwidth}
\begin{tikzpicture}
\begin{axis}[
ybar=0pt,
enlargelimits=0.15,
width=\columnwidth,
ymin=90,
ylabel={Accuracy (\%)},
symbolic x coords={VGG,GoogLeNet,DenseNet,ResNet},
xtick=data,
bar width=10pt,
legend style={
at={(0.5,-0.15)},
anchor=north,
legend columns=-1,
},
axis x line*=bottom,
axis y line*=left,
]
\addplot [blue, fill, opacity=0.7, nodes near coords,every node near coord/.append style={xshift=5pt,yshift=12pt,font=\footnotesize,rotate=90, opacity=1}]coordinates {(VGG, 93.86) (GoogLeNet, 95.22) (DenseNet, 94.85) (ResNet, 93.98)};
\addplot [green, fill, opacity=0.7, nodes near coords,every node near coord/.append style={xshift=5pt,yshift=12pt,font=\footnotesize,rotate=90, opacity=1}]coordinates {(VGG, 93.81) (GoogLeNet, 95.3) (DenseNet, 94.45) (ResNet, 93.97)};
\addplot [red, fill, opacity=0.7, nodes near coords,every node near coord/.append style={xshift=5pt,yshift=12pt,font=\footnotesize,rotate=90, opacity=1}]coordinates {(VGG, 93.86) (GoogLeNet, 95.09) (DenseNet, 94.53) (ResNet, 94.17)};
\legend{Cosine, Euclide, VBD}
\end{axis}
\end{tikzpicture}
\caption{CIFAR-100.}
\label{subfig:cifar100}
\end{subfigure}
\caption{Ablation study on distance metrics.}
\label{fig:criteria_comparison}
\end{figure}
\end{document}
以下是一条链接进行复制。
我希望图例位于图形底部,两个子图之间。这是我使用 Paint 的预期输出:
我很感激任何帮助或建议。谢谢!
答案1
这个问题看起来不像是一个真正的subfigure
问题,而是一个tikz/pgfplots
问题。
新答案
根据pgfplots
手册:
4.9.7 轴外的图例
您可以使用:
legend to name={<name>}
第一个选项中有图例axis
,第二个选项中没有图例。此选项不绘制图例但存储其代码。
然后,当您使用时,将执行此代码\ref{<name>}
。此命令甚至可以在任何命令之外使用tikzpicture
,通常是在主figure
命令之前\caption
。
\centering
在环境开始时使用命令figure
使其居中,以及subfigure
s。
旧答案:
如果您不是绝对需要\ref
单独使用 erCIFAR-10
和CIFAR-100
(使用例如\ref{fig:criteria_comparison}-left
或-right
),那么这是一个可能的解决方法:
- 抑制
sufigure
s 和subcaption
s。 - 将两个合并
tikzpicture
为一个,包含两个axis
环境。 - 将第二个放入合适的选项
axis
中scope
xshift=...
- 修改轴的锚点
legend (at=...) in the first
,并在第二个中抑制它。 - 可选
node
,在相应的axis
或中添加被抑制的子字幕作为 TikZscope
。
答案2
这是@Jhor 建议的一个可能的解决方案,如果有人需要代码,请发布:
\begin{figure}[ht!]
\centering
\begin{subfigure}{0.48\textwidth}
\begin{tikzpicture}
\begin{axis}[
ybar=0pt,
enlargelimits=0.15,
width=\columnwidth,
ymin=90,
ylabel={Accuracy (\%)},
symbolic x coords={VGG,GoogLeNet,DenseNet,ResNet},
xtick=data,
bar width=10pt,
legend style={
at={(0.5,-0.15)},
anchor=north,
legend columns=-1,
},
legend to name={mylegend},
axis x line*=bottom,
axis y line*=left,
]
\addplot [blue, fill, opacity=0.7, nodes near coords,every node near coord/.append style={xshift=5pt,yshift=12pt,font=\footnotesize,rotate=90, opacity=1}]coordinates {(VGG, 93.86) (GoogLeNet, 95.22) (DenseNet, 94.85) (ResNet, 93.98)};
\addplot [green, fill, opacity=0.7, nodes near coords,every node near coord/.append style={xshift=5pt,yshift=12pt,font=\footnotesize,rotate=90, opacity=1}]coordinates {(VGG, 93.81) (GoogLeNet, 95.3) (DenseNet, 94.45) (ResNet, 93.97)};
\addplot [red, fill, opacity=0.7, nodes near coords,every node near coord/.append style={xshift=5pt,yshift=12pt,font=\footnotesize,rotate=90, opacity=1}]coordinates {(VGG, 93.86) (GoogLeNet, 95.09) (DenseNet, 94.53) (ResNet, 94.17)};
\legend{Cosine, Euclide, VBD}
\end{axis}
\end{tikzpicture}
\caption{CIFAR-10.}
\label{subfig:cifar10}
\end{subfigure}
\hfill
%------------------------------------------------------------------------
\begin{subfigure}{0.48\textwidth}
\begin{tikzpicture}
\begin{axis}[
ybar=0pt,
enlargelimits=0.15,
width=\columnwidth,
ymin=90,
ylabel={Accuracy (\%)},
symbolic x coords={VGG,GoogLeNet,DenseNet,ResNet},
xtick=data,
bar width=10pt,
legend style={
at={(0.5,-0.15)},
anchor=north,
legend columns=-1,
},
axis x line*=bottom,
axis y line*=left,
]
\addplot [blue, fill, opacity=0.7, nodes near coords,every node near coord/.append style={xshift=5pt,yshift=12pt,font=\footnotesize,rotate=90, opacity=1}]coordinates {(VGG, 93.86) (GoogLeNet, 95.22) (DenseNet, 94.85) (ResNet, 93.98)};
\addplot [green, fill, opacity=0.7, nodes near coords,every node near coord/.append style={xshift=5pt,yshift=12pt,font=\footnotesize,rotate=90, opacity=1}]coordinates {(VGG, 93.81) (GoogLeNet, 95.3) (DenseNet, 94.45) (ResNet, 93.97)};
\addplot [red, fill, opacity=0.7, nodes near coords,every node near coord/.append style={xshift=5pt,yshift=12pt,font=\footnotesize,rotate=90, opacity=1}]coordinates {(VGG, 93.86) (GoogLeNet, 95.09) (DenseNet, 94.53) (ResNet, 94.17)};
\end{axis}
\end{tikzpicture}
\caption{CIFAR-100.}
\label{subfig:cifar100}
\end{subfigure}
\ref{mylegend}
\caption{Ablation study on distance metrics.}
\label{fig:criteria_comparison}
\end{figure}