I'm working on a LaTeX template, the elsarticle class from Elsivier, where I have two subfigures created using the subcaption package. I came across this answer which provides a solution for having a common legend for subfigures, but it uses the subfigure package. Unfortunately, I've already utilized the subcaption package for previous figures in my draft.
When I attempt to integrate the provided code into my document, I encounter errors like the ones shown in this image.
I suspect these errors may stem from conflicts between the subfigure and subcaption packages.
My main question is: How can I modify the code to have a single common legend for both subfigures while using the subcaption package?
Here's the code snippet generating the figures:
\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}
Here's a link for reproduction.
I'd like the legend to be at the bottom of the figure, between two subfigures. Here's my expected output using Paint:
I appreciate any help or suggestions. Thank you!
Best Answer
This question doesn't look as a real
subfigure
question, but atikz/pgfplots
question.New answer According to
pgfplots
manual:4.9.7 Legends Outside Of an Axis
you can use the:
legend to name={<name>}
option in the first
axis
and no legend in the second one. This option doesn't draw the legend but store its code.This code is then executed when you use
\ref{<name>}
. This command can be used even outside of anytikzpicture
, usually just before the mainfigure
's\caption
.Use
\centering
command at the beginning offigure
environmemt to get it centered, as well as thesubfigure
s.Old answer:
If you don't absolutly need to
\ref
er separatelyCIFAR-10
andCIFAR-100
(using e.g.\ref{fig:criteria_comparison}-left
or-right
), here is a possible workaround :sufigure
s and thesubcaption
s.tikzpicture
s into a single one, containing twoaxis
environments.axis
in ascope
with the suitablexshift=...
optionlegend (at=...) in the first
axis` and suppress it in the second one.node
s in the correspondingaxis
orscope
.