[Tex/LaTex] matlab2tikz and legend location

legendpgfplotstikz-pgf

I have a matlab plot given below, that is converted to tikz. Let's call it myfile.tikz

\begin{tikzpicture}
\begin{axis}[%
width=\figurewidth,
height=\figureheight,
scale only axis,
xmin=1,
xmax=3,
ymin=1,
ymax=6,
name=plot2,
legend style={at={(0.252388795928323,0.0172119972733437)},anchor=south west,legend columns=2,draw=black,fill=white,legend cell align=left}
]
\addplot [
color=blue,
solid
]
table[row sep=crcr]{
1 2\\
2 4\\
3 6\\
};
\addlegendentry{This is somewhat};

\addplot [
color=green!50!black,
solid
]
table[row sep=crcr]{
1 1\\
2 2\\
3 3\\
};
\addlegendentry{very long legend};

\end{axis}

\begin{axis}[%
width=\figurewidth,
height=\figureheight,
scale only axis,
xmin=0,
xmax=1,
ymin=0,
ymax=1,
at=(plot2.left of south west),
anchor=right of south east,
axis x line*=bottom,
axis y line*=left
]
\end{axis}

\begin{axis}[%
width=\figurewidth,
height=\figureheight,
scale only axis,
xmin=0,
xmax=1,
ymin=0,
ymax=1,
at=(plot2.right of south east),
anchor=left of south west,
axis x line*=bottom,
axis y line*=left
]
\end{axis}
\end{tikzpicture}%

A minimal example using tikz would be:

\documentclass{article}
\usepackage{pgfplots} % to use tikz graphics as given in http://www.mathworks.com/matlabcentral/fileexchange/22022-matlab2tikz
  \pgfplotsset{compat=newest}
  \pgfplotsset{plot coordinates/math parser=false}
  \newlength\figureheight
  \newlength\figurewidth

\begin{document}
\begin{figure}
    \centering
    \setlength\figureheight{3.46cm} 
    \setlength\figurewidth{3.46cm} 
    \input{myfile.tikz}
\end{figure}
\end{document}

So, as seen from the resulting document, the legend of middle figure is pushing the right one out of the frame.
I wonder if there is a way to put the legend to the bottom of the figure and also not push the adjecent axis objects.

enter image description here
enter image description here

Best Answer

A possible workaround is to use legend to name=<labelname> and \ref{<labelname>} to print the legend outside the tikzpicture. Change the axis options of the axis containing the legend entries to contain

legend to name=widelegend,
legend style={legend columns=2}

and add

\ref{widelegend}

where you want to print the legend, i.e. just after the tikzpicture.

Note that I reduced the size of the plots a little bit, to make them fit within the \textwidth.

enter image description here

Complete code:

\documentclass{article}

\usepackage{pgfplots} % to use tikz graphics as given in http://www.mathworks.com/matlabcentral/fileexchange/22022-matlab2tikz
\pgfplotsset{compat=newest}
\pgfplotsset{plot coordinates/math parser=false}
\newlength\figureheight
\newlength\figurewidth

\begin{document}
\begin{figure}
  \setlength\figureheight{3.2cm} 
  \setlength\figurewidth{3.2cm} 
\centering
\begin{tikzpicture}
\begin{axis}[%
    width=\figurewidth,
    height=\figureheight,
    scale only axis,
    xmin=1,
    xmax=3,
    ymin=1,
    ymax=6,
    name=plot2,
    legend to name=widelegend,
    legend style={legend columns=2}
]
\addplot [
   color=blue,
   solid
]
table[row sep=crcr]{
1 2\\
2 4\\
3 6\\
};
\addlegendentry{This is somewhat};

\addplot [
   color=green!50!black,
   solid
]
table[row sep=crcr]{
1 1\\
2 2\\
3 3\\
};
\addlegendentry{very long legend};

\end{axis}

\begin{axis}[%
    width=\figurewidth,
    height=\figureheight,
    scale only axis,
    xmin=0,
    xmax=1,
    ymin=0,
    ymax=1,
    at=(plot2.left of south west),
    anchor=right of south east,
    axis x line*=bottom,
    axis y line*=left
]
\end{axis}

\begin{axis}[%
    width=\figurewidth,
    height=\figureheight,
    scale only axis,
    xmin=0,
    xmax=1,
    ymin=0,
    ymax=1,
    at=(plot2.right of south east),
    anchor=left of south west,
    axis x line*=bottom,
    axis y line*=left
]
\end{axis}
\end{tikzpicture}
\ref{widelegend}
\end{figure}
\end{document}
Related Question