9

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}
4
  • 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}.
    – jub0bs
    May 18, 2013 at 17:54
  • @Jubobs: It's done.
    – juliohm
    May 18, 2013 at 19:20
  • 1
    This looks like a bug. Would you mind filing a bug report for this?
    – Jake
    May 18, 2013 at 20:57
  • 1
    @Jake: Thanks, it's done: sourceforge.net/tracker/…
    – juliohm
    May 19, 2013 at 11:44

2 Answers 2

8

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}

enter image description here

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).

2

An example, from pgfplots gallery:

enter image description here

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:

enter image description here

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

3
  • Although a 3D plot, it doesn't add legend to contour plots. Thank you for the answer.
    – juliohm
    May 18, 2013 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, 2013 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.
    – user13907
    May 19, 2013 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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