# How to add legend to contour plots?

The following does not work:

\documentclass{standalone}
\usepackage{tikz,pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[view={0}{90},legend entries={foo,bar}] % 'legend entries' causes compilation error
\addplot3[contour gnuplot={draw color=red,labels=false}]  {x*y};
\addplot3[contour gnuplot={draw color=blue,labels=false}] {x^2 + y^2};
\end{axis}
\end{tikzpicture}
\end{document}

-
Please add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. –  Jubobs May 18 '13 at 17:54
@Jubobs: It's done. –  juliohm May 18 '13 at 19:20
This looks like a bug. Would you mind filing a bug report for this? –  Jake May 18 '13 at 20:57
@Jake: Thanks, it's done: sourceforge.net/tracker/… –  juliohm May 19 '13 at 11:44

This is a bug (thanks for the report).

A valid work-around might be to reconfigure the legend for this plot:

\documentclass{standalone}
\usepackage{tikz,pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[view={0}{90},
legend image post style={
sharp plot,
draw=\pgfkeysvalueof{/pgfplots/contour/draw color},
},
legend entries={foo,bar}] % 'legend entries' causes compilation error
\addplot3[contour gnuplot={draw color=red,labels=false}]  {x*y};
\addplot3[contour gnuplot={draw color=blue,labels=false}] {x^2 + y^2};
\end{axis}
\end{tikzpicture}
\end{document}


The legend image post style means to interprete the coordinates of the current legend style as a normal "sharp plot". The draw statement retrieves the current value of the contour color setting (which matches your use-case here).

-

An example, from pgfplots gallery:

A non-elegant solution:

\documentclass{standalone}
\usepackage{tikz,pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[view={0}{90}] % 'legend entries' causes compilation error
\addplot3[contour gnuplot={draw color=red,labels=false}]  {x*y};
\addplot3[contour gnuplot={draw color=blue,labels=false}] {x^2 + y^2};
\addplot [blue, mark = *, nodes near coords=foo,every node near coord/.style={anchor=180}] coordinates {( 3,1)};
\addplot [red, mark = *, nodes near coords=bar,every node near coord/.style={anchor=180}] coordinates {(0,1)};
\end{axis}
\end{tikzpicture}
\end{document}


Result:

It is important to consider that classic legend in contour graphics can hide important informations.

-
Although a 3D plot, it doesn't add legend to contour plots. Thank you for the answer. –  juliohm May 18 '13 at 19:22
@Papiro: Regarding your final sentence: Do you mean that if the legend is placed within the axis limits, you could cover up some of the contour lines? In that case, I believe you should just move the legend outside the axis limits (it's not really a problem that's specific to contour plots). Using a classic legend has the advantage that it's clear what it refers to: All red lines are "bar", all blue lines are "foo". Using dots and labels like in your example are somewhat ambiguous (does "bar" refer to that one point, or to that one line, or to all red lines?). –  Jake May 19 '13 at 17:20
@Jake Good points! The ambiguity can be avoided by using the figure caption (and small points for "foo" and "bar", of course). I think that the legend outside the axis limits is not an aesthetic option. –  Papiro May 19 '13 at 17:39