I'm using pgfplots with tikz's external library for my graphics, and I must use the subfigure package for compliancy reasons.
As you can see in the mwe below (part "Attempt 1"), inside a figure* environment (which I must use) I place two small subfigures, and I would like the legend (common to both figures) to be between them, possibly at mid-height. However, even though I tried various legend position specifications, no matter what I do the legend doesn't move by a single millimetre.
\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[pdftex]{graphicx}
\graphicspath{{./img/}}
\DeclareGraphicsExtensions{.pdf}
\usepackage{subfigure}
\usepackage{pgfplots}
\usetikzlibrary{external}
\tikzexternalize
\tikzsetexternalprefix{tikzpics/}
\pgfplotsset{compat=newest, every mark/.append style={mark size=1pt, mark line=solid}}
\begin{document}
%%%%%%%%% Attempt 1
\begin{figure*}[t!]
\centering
\subfigure[First]{
\begin{tikzpicture}
\begin{axis}[width=0.4\textwidth,xlabel={X axis}, ylabel={Y axis},]
\addplot [blue, mark=diamond*, densely dashed, ultra thick, mark options={solid,scale=1.5}] coordinates {(-0.5,-1) (1,1)};
\addplot [green, mark=*, ultra thick] coordinates {(-0.5,-0.9) (1,1.1)};
\addplot [red, mark=square*, dotted, ultra thick] coordinates {(-0.5,-1.1) (1,0.9)};
\end{axis}
\end{tikzpicture}}\hfil
\subfigure{
\begin{tikzpicture}
\begin{axis}[width=0.2\textwidth, height=0.2\textwidth, hide axis, xmin=0, xmax=10, ymin=0, ymax=0.2,legend columns=1,
legend style={draw=white!15!black,legend cell align=left, at={(0.5,0.5)}},]
\addlegendimage{blue, mark=diamond*, densely dashed, ultra thick, mark options={solid,scale=1.5}}
\addlegendentry{Trace 1}
\addlegendimage{green, mark=*, ultra thick}
\addlegendentry{Trace 2}
\addlegendimage{red, mark=square*, dotted, ultra thick, mark options={solid,scale=1.5}}
\addlegendentry{Trace 3}
\end{axis}
\end{tikzpicture}}\hfil
\subfigure[Second]{
\begin{tikzpicture}
\begin{axis}[width=0.4\textwidth,xlabel={X axis}, ylabel={Y axis},]
\addplot [blue, mark=diamond*, densely dashed, ultra thick, mark options={solid,scale=1.5}] coordinates {(-0.5,-1) (1,1)};
\addplot [green, mark=*, ultra thick] coordinates {(-0.5,-0.9) (1,1.1)};
\addplot [red, mark=square*, dotted, ultra thick] coordinates {(-0.5,-1.1) (1,0.9)};
\end{axis}
\end{tikzpicture}}
\caption{Caption.}
\end{figure*}
%%%%%%%%% Attempt 2
\begin{figure*}[t!]
\centering
\subfigure[First]{
\begin{tikzpicture}
\begin{axis}[width=0.4\textwidth,xlabel={X axis}, ylabel={Y axis}, legend style={overlay, at={(1.2,0.5)},anchor={north}}]
\addplot [blue, mark=diamond*, densely dashed, ultra thick, mark options={solid,scale=1.5}] coordinates {(-0.5,-1) (1,1)};
\addplot [green, mark=*, ultra thick] coordinates {(-0.5,-0.9) (1,1.1)};
\addplot [red, mark=square*, dotted, ultra thick] coordinates {(-0.5,-1.1) (1,0.9)};
\legend{Trace 1, Trace 2, Trace 3};
\end{axis}
\end{tikzpicture}}\hfil
\subfigure[Second]{
\begin{tikzpicture}
\begin{axis}[width=0.4\textwidth,xlabel={X axis}, ylabel={Y axis},]
\addplot [blue, mark=diamond*, densely dashed, ultra thick, mark options={solid,scale=1.5}] coordinates {(-0.5,-1) (1,1)};
\addplot [green, mark=*, ultra thick] coordinates {(-0.5,-0.9) (1,1.1)};
\addplot [red, mark=square*, dotted, ultra thick] coordinates {(-0.5,-1.1) (1,0.9)};
\end{axis}
\end{tikzpicture}}
\caption{Caption.}
\end{figure*}
\end{document}
As a second attempt, I tried to use the overlay option of the axis environment (see part "Attempt 2" in the mwe above). This time, I see that the subfigure environment clips away the portion of the legend which lies outside the plot (see image below), and I can't seem to find any option to avoid this.
Just to mention it, I also gave a try to using pgfplots' option legend to name+\ref, but even by playing with the various options for externalization I could not succeed.
Any suggestion is welcomed, thanks in advance.



axisusinglegend pos=outer north east. By that you avoid the problem of your attempt 1 which will resulted in sublabels (a) and (c) (instead of (b)) and the need to manually shift the "extra" legend up(wards) ... – Stefan Pinnow Aug 19 '19 at 12:40subfigureso that thetikzpicturewith the legend is "standalone" (so now the second plot is correctly labelled (b), thanks @StefanPinnow); secondly, I put the legend inside\raisebox{Xcm}{}to adjust its vertical position. I can now obtain what I wanted, but still doesn't seem an optimal solution – DavideM Aug 20 '19 at 11:10