Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I thought I can add a legend to a tikz picture by \addlegendentry{...}. But as soon as I uncomment the command the compiler complains about missing }. But when I comment the lines again, then it works. Could somebody give me a hint, why the legend doesn't work?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows, intersections}
\usepackage{pgfplots}
 \pgfplotsset{compat=newest}
  \pgfplotsset{plot coordinates/math parser=false}
%from matlab2tikz
   \newlength\figureheight
   \newlength\figurewidth 


\begin{document}
Sample plot with out legend?

\begin{figure}       
        \setlength\figureheight{2cm}
        \setlength\figurewidth{2.7cm} 
               { 


\begin{tikzpicture}[font=\tiny]
\begin{axis}[%
width=\figurewidth,
height=\figureheight,
scale only axis,
xmin=1,
xmax=20,
ymin=0,
ymax=2,
title={Sample time series}, 
legend style={draw=black,fill=white,legend cell align=left}
]
\addplot [color=blue,solid,forget plot]
  table[row sep=crcr]{1 1\\
2       1\\
3       1\\
4       1\\
5       1\\
6       1\\
7       1\\
8       1\\
9       1\\
10      2\\
11      1\\
12      1.5\\
13      0\\
14      1\\
15      1\\
16      1\\
17      1\\
18      1\\
19      1\\
20      1\\
};
\addplot [color=green,dashed ,forget plot]
  table[row sep=crcr]{8 0\\
  8 2\\
  10 2\\
  10 0\\
};
%\addlegendentry{Window size=2};
\addplot [color=violet,densely dotted ,forget plot]
  table[row sep=crcr]{10.5 0\\
  10.5 2\\
  15.5 2\\
  15.5 0\\
};
%\addlegendentry{Window size=5};
\end{axis}
\end{tikzpicture}%

                    }

\end{figure} 
\end{document}
share|improve this question

1 Answer 1

up vote 4 down vote accepted

Don't use forget plot for plots for which you want to add a legend (forget plot is for plots which shouldn't be remembered for legend entries nor considered for cycle lists):

\documentclass[twoside]{article}
\usepackage{pgfplots}

\newlength\figurewidth
\setlength\figurewidth{6cm}
\newlength\figureheight
\setlength\figureheight{6cm}

\begin{document}

\begin{tikzpicture}[font=\tiny]
\begin{axis}[%
width=\figurewidth,
height=\figureheight,
scale only axis,
xmin=1,
xmax=20,
ymin=0,
ymax=2,
title={Sample time series}, 
legend style={draw=black,fill=white,legend cell align=left}
]
\addplot [color=blue,solid,forget plot]
  table[row sep=crcr]{1 1\\
2       1\\
3       1\\
4       1\\
5       1\\
6       1\\
7       1\\
8       1\\
9       1\\
10      2\\
11      1\\
12      1.5\\
13      0\\
14      1\\
15      1\\
16      1\\
17      1\\
18      1\\
19      1\\
20      1\\
};
\addplot [color=green,dashed ]
  table[row sep=crcr]{8 0\\
  8 2\\
  10 2\\
  10 0\\
};
\addlegendentry{Window size=2}
\addplot [color=violet,densely dotted ]
  table[row sep=crcr]{10.5 0\\
  10.5 2\\
  15.5 2\\
  15.5 0\\
};
\addlegendentry{Window size=5}
\end{axis}
\end{tikzpicture}%

\end{document}

enter image description here

share|improve this answer
    
Thx for the quick solution. Seems like that importing via matlab2tikz and editing afterwards is perhaps not the very best idea ;) Anyway thx a lot. –  LeO May 17 at 16:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.