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.
Best Answer
A possible workaround is to use
legend to name=<labelname>
and\ref{<labelname>}
to print the legend outside thetikzpicture
. Change theaxis
options of the axis containing the legend entries to containand add
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
.Complete code: