PGFPlots:将外部图例与相邻的子图对齐

PGFPlots:将外部图例与相邻的子图对齐

我正在使用pgfplotstikzexternal来处理我的图形,并且subfigure出于合规性原因我必须使用该包。

正如您在下面的 mwe 中看到的(“尝试 1” 部分),在一个figure*环境中(我必须使用),我放置了两个小子图,并且我希望图例(两个图例共有)位于它们之间,可能位于中间高度。但是,即使我尝试了各种图例位置规范,无论我做什么,图例都不会移动一毫米。

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

尝试 1

作为第二次尝试,我尝试使用overlay环境选项axis(参见上面 mwe 中的“尝试 2”部分)。这一次,我看到环境subfigure剪掉了图例中位于图外的部分(参见下图),我似乎找不到任何选项来避免这种情况。

第二次尝试

顺便提一下,我也尝试使用pgfplots“选项legend to name+” \ref,但即使使用各种外部化选项,我也无法成功。

欢迎提出任何建议,提前致谢。

答案1

您可以使用sbaseline选项tikzpicture,这似乎也适用于外部图形。我添加了(或 )选项name=ax,并对所有三个选项都使用了 。我还删除了中间的,因为它不需要。axislegend stylebaseline=(ax.center)\subfigure{}tikzpicture

补充一下,在这种情况下,制作自定义循环列表可能会很有用,这样您就不必重复三次绘图样式。

在此处输入图片描述

\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},
}
% create a custom cycle list:
\pgfplotscreateplotcyclelist{MyList}{
blue, mark=diamond*, densely dashed, ultra thick, mark options={solid,scale=1.5}\\
green, mark=*, ultra thick\\
red, mark=square*, mark options={solid}, mark size=3pt,dotted, ultra thick\\
}
\begin{document}


\begin{figure*}
\centering
\subfigure[First]{
\begin{tikzpicture}[baseline=(ax.center)] % <-- added baseline
    \begin{axis}[
      name=ax, % added
      cycle list name=MyList, % added
      width=0.4\textwidth,
      xlabel={X axis},
      ylabel={Y axis}
]
        \addplot coordinates {(-0.5,-1) (1,1)};
        \addplot coordinates {(-0.5,-0.9) (1,1.1)};
        \addplot coordinates {(-0.5,-1.1) (1,0.9)};
    \end{axis}
\end{tikzpicture}}\hfil
\begin{tikzpicture}[baseline=(leg.center)] % <-- added baseline
\begin{axis}[
   cycle list name=MyList, % added
   width=0.2\textwidth, height=0.2\textwidth,
   hide axis,
   xmin=0, xmax=10, ymin=0, ymax=0.2,
   legend columns=1,
   legend style={ 
     name=leg, % added
     draw=white!15!black,
     legend cell align=left,
      at={(0.5,0.5)}
     }]

    % add three dummy plots, the single point is outside the axis limits, so not shown
    \pgfplotsinvokeforeach{1,2,3}{\addplot coordinates {(0,-1)};}
    \addlegendentry{Trace 1}
    \addlegendentry{Trace 2}
    \addlegendentry{Trace 3}
\end{axis}
\end{tikzpicture}\hfil
\subfigure[Second]{
\begin{tikzpicture}[baseline=(ax.center)] % <-- added baseline option
    \begin{axis}[
       name=ax,  % added
       cycle list name=MyList, % added
       width=0.4\textwidth,
       xlabel={X axis}, ylabel={Y axis},
     ]
        \addplot coordinates {(-0.5,-1) (1,1)};
        \addplot coordinates {(-0.5,-0.9) (1,1.1)};
        \addplot coordinates {(-0.5,-1.1) (1,0.9)};
    \end{axis}
\end{tikzpicture}}
\caption{Caption.}
\end{figure*}
\end{document}

相关内容