How to display tikz picture between subfigure and subcaption?
Just save some coordinates with remember picture
and use overlay,remember picture
to draw the bar. You do not need a third picture, see the second variant.
\documentclass[11pt]{article}
\usepackage{tikz}
\usepackage{subcaption}
\usepackage{caption}
\begin{document}
\begin{figure}
\centering
\begin{subfigure}{0.45\textwidth}
\centering
\begin{tikzpicture}[remember picture]
\fill[black] (0,0) coordinate(L-sw) rectangle (3,3) ;
\end{tikzpicture}
\captionsetup{skip=30pt}
\caption{}
\end{subfigure}%
\begin{subfigure}{0.45\textwidth}
\centering
\begin{tikzpicture}[remember picture]
\fill[black] (3,0) coordinate(R-se) rectangle (0,3);
\end{tikzpicture}
\captionsetup{skip=30pt}
\caption{}
\end{subfigure}
\begin{tikzpicture}[overlay,remember picture]
\fill[red] ([yshift=-3mm,xshift=4mm]L-sw) rectangle
([yshift=-8mm,xshift=-4mm]R-se);
\end{tikzpicture}
\caption{Caption}
\end{figure}
\begin{figure}
\centering
\begin{subfigure}{0.45\textwidth}
\centering
\begin{tikzpicture}[remember picture]
\fill[black] (0,0) coordinate(L-sw) rectangle (3,3) ;
\end{tikzpicture}
\captionsetup{skip=30pt}
\caption{}
\end{subfigure}%
\begin{subfigure}{0.45\textwidth}
\centering
\begin{tikzpicture}[remember picture]
\fill[black] (3,0) coordinate(R-se) rectangle (0,3);
\fill[red,overlay] ([yshift=-3mm,xshift=4mm]L-sw) rectangle
([yshift=-8mm,xshift=-4mm]R-se);
\end{tikzpicture}
\captionsetup{skip=30pt}
\caption{}
\end{subfigure}
\caption{Caption}
\end{figure}
\end{document}
As for your updated question, just use legend to name=named
to store the legend, and \ref{named}
to recycle it.
\documentclass[11pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepackage{subcaption}
\usepackage{caption}
\begin{document}
\begin{figure}
\centering\begin{tikzpicture}
\begin{axis}[hide axis, xmin=10, xmax=50, ymin=0, ymax=0.4,
legend style={legend cell align=left, legend columns=-1},
legend to name=named]
\addlegendimage{blue}
\addlegendentry{example 1}
\addlegendimage{red}
\addlegendentry{example 2}
\end{axis}
\end{tikzpicture}%
\begin{subfigure}{0.45\textwidth}
\centering
\begin{tikzpicture}[remember picture]
\node[inner sep=0pt] (L){\includegraphics[width=\linewidth]{example-image-a}};
\end{tikzpicture}
\captionsetup{skip=30pt}
\caption{}
\end{subfigure}%
\begin{subfigure}{0.45\textwidth}
\centering
\begin{tikzpicture}[remember picture]
\node[inner sep=0pt] (R){\includegraphics[width=\linewidth]{example-image-b}};
\end{tikzpicture}
\captionsetup{skip=30pt}
\caption{}
\end{subfigure}
\begin{tikzpicture}[overlay,remember picture]
\path (L.south)--(R.south) node[midway,below=1ex] {\ref{named}};
\end{tikzpicture}
\caption{Caption}
\end{figure}
\end{document}
Similar idea as in @Schrödinger's cat answer (use of overlay, remember picture
), but with use of \tikzmatk
library for determining of anchors of red rectangle:
Eddit:
with legend instead of simple rectangle. For simplicity it is drawn as pure tikz
image:
\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{tikzmark} % <---
\usepackage{graphicx} % <---
\usepackage[skip=1ex]{caption}
\usepackage{subcaption}
\begin{document}
\begin{figure}
\centering
\captionsetup[subfigure]{skip=6ex} % <--- <---
\begin{subfigure}{0.45\textwidth}
\tikzmarknode{A}{\includegraphics[width=\linewidth]{example-image-duck}} % <---
\caption{}
\label{subfig:a}
\end{subfigure}
\hfil
\begin{subfigure}{0.45\textwidth}
\tikzmarknode{B}{\includegraphics[width=\linewidth]{example-image-duck}} % <---
\caption{}
\label{subfig:b}
\end{subfigure}
\begin{tikzpicture}[overlay, remember picture,
node distance=1ex and 0ex]
\node (n1) [below left=of $(A.south)!0.5!(B.south)$] {example 1};
\draw[blue, thick] (n1.west) -- ++ (-1,0) coordinate (aux1);
\draw[red, thick] (n1.east) -- ++ (+1,0) node (n2) [right] {example 2};
\node[draw, inner ysep=1pt, fit=(aux1) (n2)] {};
\end{tikzpicture}
\caption{Caption}
\end{figure}
\end{document}