[Tex/LaTex] Add legend to pgf plot results in \end{axis} error

legendmatlab2tikztikz-pgf

I exported a Matlab figure to tikz, using matlab2tikz. It is a simple plot of some lines and I need to label them. matlab2tikz did not add the legend entries automatically for the first time, so I copy/pasted the legend command from another pgf plot. It was also the first time I used it in Matlab2014. This is just FYI, but probably unrelated to the pgf problem.

Below I added the tikz/pgf code. It compiles if I comment the legend entries, but when I uncomment them I get the following error:

Missing } inserted \end{axis}

It refers to the line of \end{axis}, and a bunch of other error messages follow.

Here's the code:

% This file was created by matlab2tikz v0.4.4 running on MATLAB 8.4.
% Copyright (c) 2008--2013, Nico Schlömer <nico.schloemer@gmail.com>
% All rights reserved.
% 
% The latest updates can be retrieved from
%   http://www.mathworks.com/matlabcentral/fileexchange/22022-matlab2tikz
% where you can also make suggestions and rate matlab2tikz.
% 
%
% defining custom colors

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\newlength\figurewidth
\setlength{\figurewidth}{\textwidth}

\definecolor{mycolor1}{rgb}{0,0.447,0.741}%
\definecolor{mycolor2}{rgb}{0.85,0.325,0.098}%
\definecolor{mycolor3}{rgb}{0.929,0.694,0.125}%
\definecolor{mycolor4}{rgb}{0.494,0.184,0.556}%
\definecolor{mycolor5}{rgb}{0.466,0.674,0.188}%
%

\begin{document}
\begin{tikzpicture}

\begin{axis}[%
width=\figurewidth,
height=0.535665322580645\figurewidth,
unbounded coords=jump,
scale only axis,
every outer x axis line/.append style={darkgray!60!black},
every x tick label/.append style={font=\color{darkgray!60!black}},
xmin=-10,
xmax=10,
xlabel={Parameter},
every outer y axis line/.append style={darkgray!60!black},
every y tick label/.append style={font=\color{darkgray!60!black}},
ymode=log,
ymin=1e-06,
ymax=1000,
yminorticks=true,
ylabel={Error},
axis x line    *=bottom,
axis y line    *=left,
legend style={draw=darkgray!60!black,fill=white,legend cell align=left}
]
\addplot [
color=mycolor1,
solid,
forget plot
]
table[row sep=crcr]{
-10 0.257539245299513\\
10 0.278411090059315\\
};
\addlegendentry{Case 1};

\addplot [
color=mycolor2,
dashed,
forget plot
]
table[row sep=crcr]{
-10 0.0371047553354069\\
10 0.0338867959948958\\
};
\addlegendentry{Case 2};

\addplot [
color=mycolor3,
dotted,
forget plot
]
table[row sep=crcr]{
-10 0.0109546932939792\\
10 0.00673477512625754\\
};
\addlegendentry{Case 3};

\addplot [
color=mycolor4,
dash pattern=on 1pt off 3pt on 3pt off 3pt,
forget plot
]
table[row sep=crcr]{
-10 0.0010526474053356\\
10 0.000764939997185852\\
};
\addlegendentry{Case 4};

\addplot [
color=mycolor5,
solid,
mark=+,
mark options={solid},
forget plot
]
table[row sep=crcr]{
-10 0.000686338275055355\\
10 0.000758083080815557\\
};
\addlegendentry{Case 5};

\end{axis}
\end{tikzpicture}%
\end{document}

Thanks for any useful insights 😉

Best Answer

Expanding my comment to an answer.

The problem is in the use of forget plot. Due to the fact pgfplots shall forget each plot, but on the other hand defining a legend entry for each plot, pgfplots doesn't know what to do. To solve the problem two possibilities are given, based on the desired result.

  1. Remove all \addlegendentry from the different plots, so no legend will be plotted.

  2. Remove -at least one- forget plot to plot the legend.

Related Question