如何在子图和子标题之间显示 tikz 图片?

如何在子图和子标题之间显示 tikz 图片?

我想在多个子图及其子标题之间插入使用 tikzpicture 环境生成的图例。我当前的代码是

\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}

看起来像这样。在此处输入图片描述

有谁知道如何在子图和子标题之间显示 tikzpicture?

这就是我希望它最终呈现的样子: 在此处输入图片描述

答案1

与@Schrödinger's cat answer中的想法类似(使用overlay, remember picture),但使用\tikzmatk库来确定红色矩形的锚点:

編輯: 用图例代替简单的矩形。为简单起见,它被绘制为纯tikz图像:

\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}

在此处输入图片描述

答案2

只需保存一些坐标remember picture并用来overlay,remember picture绘制条形图即可。您不需要第三张图片,请参阅第二种方案。

\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}

在此处输入图片描述

至于您更新的问题,只需用来legend to name=named存储图例并\ref{named}回收它。

\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}

在此处输入图片描述

相关内容