0

How can i change the legend position in a multi-columns paper? Is there anything wrong with my code? I would like to put the legend in the top center of those two figures.

Thanks.

\begin{figure}[t]
\ref{testLegend}
\subfigure[Figure 1]{
\begin{tikzpicture}
\begin{axis}[xlabel={x}, ylabel={y}, width=4cm, ylabel near ticks, xlabel near ticks, y label style={font=\small}, x label style={font=\small}, ymajorgrids, cycle list name=exotic]
\addplot coordinates {(0,0) (1,1)};
\addplot coordinates {(0,0) (1,2)};
\end{axis}
\end{tikzpicture}
\label{fig:a}
}
\vspace{-1em}
\subfigure[Figure 2]{
\begin{tikzpicture}
\begin{axis}
[xlabel={x}, ylabel={y}, width=4cm, ylabel near ticks, xlabel near ticks, y label style={font=\small}, x label style={font=\small}, legend entries={line1, line2}, legend columns=2, legend to name=testLegend, legend style={font=\scriptsize}, legend pos=north east, legend style={font=\small}, ymajorgrids, cycle list name=exotic]
\addplot coordinates {(0,0) (1,1)};
\addplot coordinates {(0,0) (1,2)};
\end{axis}
\end{tikzpicture}
\label{fig:b}
} 
\caption{Caption test, \label{fig:c}}
\vspace{-1.5em}
\end{figure}

enter image description here

However, i want this result,

enter image description here

Polaris
  • 101
  • unrelated but never use \center the command form is \centering – David Carlisle Jan 22 '21 at 17:39
  • From your use of \subfigure I guess, you currently use the subfigure package. Please be aware that this package si considered obsolete. Instead, you can either use the subfig or the subcaption package. – leandriis Jan 22 '21 at 18:53
  • Since you seem to be unsatisfied with the current position of the legend: what exactly does "top center of those two figures" refer to? Probably a sketch of the expected output would help here? Please also make your code compilable by adding the documentclass as well as the relevant package. (See also: minimal working example (MWE)) – leandriis Jan 22 '21 at 18:55

1 Answers1

1

With a custom edfined axis style to avoid repetition, subfig instead of the deprecated subfigure package and a separate tikzpicture for the legend:

enter image description here

\documentclass[twocolumn]{article}
\usepackage{pgfplots}
\usepackage{subfig}

\pgfplotsset{ compat=1.17, my axis style/.style={ xlabel={x}, ylabel={y}, width=4cm, ylabel near ticks, xlabel near ticks, y label style={font=\small}, x label style={font=\small}, ymajorgrids, cycle list name=exotic }, }

\usepackage{lipsum} % For dummy text using the \lipsum command. Do not use in actual document. \begin{document}

\begin{figure} \centering

\begin{tikzpicture} \node[draw=black, inner sep=6pt]{\ref{plot:teal} line1 \ref{plot:orange} line2}; \end{tikzpicture}

\subfloat[Figure 1]{ \begin{tikzpicture} \begin{axis}[my axis style] \addplot coordinates {(0,0) (1,1)}; \label{plot:teal} \addplot coordinates {(0,0) (1,2)}; \label{plot:orange} \end{axis} \end{tikzpicture} \label{fig:a} } \hfill \subfloat[Figure 2]{ \begin{tikzpicture} \begin{axis}[my axis style] \addplot coordinates {(0,0) (1,1)}; \addplot coordinates {(0,0) (1,2)}; \end{axis} \end{tikzpicture} \label{fig:b} } \caption{Caption test, \label{fig:c}} \end{figure}

\lipsum[1-5] \end{document}

leandriis
  • 62,593