4

I want to insert a legend generated with the tikzpicture environment between multiple subfigures and their subcaptions. My current code is

\documentclass[11pt]{article}
\usepackage{tikz}
\usepackage{subcaption}
\usepackage{caption}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}

\begin{figure}
\centering
\begin{subfigure}{0.45\textwidth}
\centering
\includegraphics[width=\linewidth]{example-image-a}
\captionsetup{skip=30pt}
\caption{Graph A}
\end{subfigure}%
\begin{subfigure}{0.45\textwidth}
\centering
\includegraphics[width=\linewidth]{example-image-b}
\captionsetup{skip=30pt}
\caption{Graph B}
\end{subfigure}
\begin{tikzpicture} 
    \begin{axis}[hide axis, xmin=10, xmax=50, ymin=0, ymax=0.4,
legend style={legend cell align=left, legend columns=-1}]
    \addlegendimage{blue}
    \addlegendentry{example 1}
    \addlegendimage{red}
    \addlegendentry{example 2}
    \end{axis}
\end{tikzpicture}
\caption{Caption}
\end{figure}
\end{document}

and looks like this. enter image description here

Does anyone know a way to display the tikzpicture between the subfigures and subcaptions?

This is what I want it to look like in the end: enter image description here

3

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}

enter image description here

  • Thanks, this looks great! Do you know of any way to set the anchors of my axis environment instead of the \fill? – user2489306 Dec 14 '19 at 12:50
  • @user2489306, you like to have a tikz or pgfplots image instead of simple rectangle?Can you gives to us some tip about image or even better code of this image? – Zarko Dec 14 '19 at 14:09
  • I added the code and images to my original question. – user2489306 Dec 14 '19 at 14:18
3

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}

enter image description here

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}

enter image description here

  • Unfortunately my question was not clear enough, but the two images above are actually included graphics. I updated the question code/example images to reflect my problem a little better, do you think there is some way to adjust your solution for this case? – user2489306 Dec 14 '19 at 12:06
  • @user2489306 I added an answer to your updated question which uses the legend which you create. – Schrödinger's cat Dec 14 '19 at 15:41
  • Thank you, works great! – user2489306 Dec 16 '19 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.