PGFPlots:將外部圖例與相鄰子圖對齊

PGFPlots:將外部圖例與相鄰子圖對齊

我正在使用pgfplotswithtikzexternal庫來製作圖形,並且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環境剪掉了位於圖之外的圖例部分(見下圖),我似乎找不到任何選項來避免這種情況。

嘗試2

順便提一下,我也嘗試過使用pgfplots' option legend to name+ \ref,但即使透過使用各種外部化選項,我也無法成功。

歡迎任何建議,提前致謝。

答案1

您可以使用sbaseline的選項tikzpicture,似乎也適用於外部化圖形。我添加name=axaxis選項(或legend style),並用於baseline=(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}

相關內容