2

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

1
  • This question may be similar to this but definitely not a duplicate.
    – Ali
    Commented Nov 8, 2013 at 13:31

2 Answers 2

4

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}
3
  • This works nicely. For externalization, use \tikzexternaldisable\ref{widelegend}\tikzexternalenable instead of \ref{widelegend}. taken from here
    – Ali
    Commented Nov 8, 2013 at 20:39
  • What should I do if I want to put legend at the top right corner? @Torbjorn T.
    – Khaaba
    Commented Jul 1, 2014 at 12:48
  • @Khaaba For a case just like this? Something like adding name=plot3 to the options of the third axis, and then adding \node [right] at (plot3.north east) {\ref{widelegend}}; just before \end{tikzpicture}. (For a better answer, please ask a new question. More people will see it, and you have more room to state clearly what you want. You can always refer back to this question if necessary.) Commented Jul 1, 2014 at 13:02
1

@Torbjon T., whether OP[Ali] likes it or not, I made the following changes[plz refer to % <<< ... >>> in the code: MWE] to suit my purpose and it gives me a good result. Thanks a lot for wonderful techniques that made me try different options and I found this new method:

%\documentclass{article} %<<<< CHANGES MADE HERE: Commented >>>>
\documentclass[preview,border=7pt]{standalone} %<<<< CHANGES MADE HERE: Added >>>>
\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,  %<<<< CHANGES MADE HERE: Commented >>>>
%    legend style={legend columns=2} %<<<< CHANGES MADE HERE: Commented >>>>
legend style={at={(1.1,-0.2)}} %<<<< CHANGES MADE HERE: Added >>>>
]
\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}%<<<< CHANGES MADE HERE: Commented >>>>
\end{figure}
\end{document}

I did not use the three commands:

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

rather used one command:

legend style={at={(1.1,-0.2)}}

by manually specifying the location of the legend position.

enter image description here

1
  • Works fine as long as the legend entries are short enough. For example, try changing the first legend entry to \addlegendentry{This is a somewhat extremely long legend-thingy.};. Commented Jul 2, 2014 at 12:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .