# ContourPlot refuses to show in-figure PlotLegends after turn on ContourShading

I would like to do a contour plot with a label inside the figure. For example, the following works well. Note there is a small "example" at the right bottom figure.

ContourPlot[Cos[x] + Cos[y], {x, 0, 4 Pi}, {y, 0, 4 Pi},
PlotLegends -> Placed["example", {0.8, 0.1}]]


However, I don't want contour shading. But after I set ContourShading -> None, the PlotLegends disappeared.

ContourPlot[Cos[x] + Cos[y], {x, 0, 4 Pi}, {y, 0, 4 Pi},
PlotLegends -> Placed["example", {0.8, 0.1}], ContourShading -> None]


Would you be so kind to see if it is a bug? And is there a workaround?

(I am using Mathematica 9.0.1)

-

The plots you are producing by adding PlotLegends all have Head of Legended. So the closest to what you already have would be to do the following:

Legended[
ContourPlot[
Cos[x]+Cos[y],{x,0,4 Pi},{y,0,4 Pi},
],
Placed["example",{0.8,0.1}]
]


This produces an output that is of the same type as your plot with contour shading turned on, i.e., Legended. The plot looks exactly the same as the colored one, and the legend is visible now because we're overriding the decision that Mathematica incorrectly made when it dropped the PlotLegend just because there were no colors to assign to your legend.

-

I don't think this is entirely unexpected since PlotLegends is meant to depict what the colours mean. You switch off the colours and the plot legend disappears. The canonical way to leave the "example" in place is to use an epilog:

p = ContourPlot[Cos[x] + Cos[y], {x, 0, 4 Pi}, {y, 0, 4 Pi},
Epilog -> Text["example", Scaled[{0.8, 0.1}]]]


---EDIT---

What @Jens said - Mathematica drops the plot legend (in my opinion, not incorrectly) because no colors are assigned to the plot. Trying

p1 = ContourPlot[Cos[x] + Cos[y], {x, 0, 4 Pi}, {y, 0, 4 Pi},
Epilog -> Text["example", Scaled[{0.8, 0.1}]],
PlotLegends -> Automatic]


gives you a legended plot (with the "example" in place). If you want to just label the plot with the word "example" then I think the right way to go is the way I posted above (p). When I say the "right" way, I mean the way that gives you a Graphics object with the expected structure. But trying this:

p3 = Show[ContourPlot[Cos[x] + Cos[y], {x, 0, 4 Pi}, {y, 0, 4 Pi},
Graphics[Text["example", Scaled[{0.8, 0.1}]]]]


gives you the same output with slightly different structure:

Head /@ {p, p2, p3}

(* {Graphics, Legended, Graphics} *)


and

Depth /@ {p, p2, p3}

(* {11, 12, 11} *)


and

GraphicsRow[Graphics /@ {p[[1]], p2[[1, 1]], p3[[1]]}]


-
Thank you very much! This looks very nice. Would you explain a bit about "PlotLegends is meant to depict what the colours mean"? I don't understand it and am interested in learning about it :) –  Yi Wang Apr 18 '14 at 19:15
I've added a bit but I don't know if I made it more or less clear :) –  gpap Apr 19 '14 at 9:00

Let me give a workaround myself after some attempts. It is only a workaround thus other answers are appreciated!

ContourPlot[Cos[x] + Cos[y], {x, 0, 4 Pi}, {y, 0, 4 Pi},
PlotLegends -> Placed["example", {0.8, 0.1}],
ColorFunction -> (White &)]


This works. In other words, here the contour shading is not turned off, but instead colored as White.

A significant disadvantage of this workaround is that, Mathematica is actually plotting everything (in white color). Thus when PlotRange is All or Automatic, the figure could contain large white space (not in here but in my actually usage). I have to adjust PlotRange myself by hand...

-

Another workaround is to create the plot as normal and then delete all the Polygon expressions:

ContourPlot[Cos[x] + Cos[y], {x, 0, 4 Pi}, {y, 0, 4 Pi},
PlotLegends -> Placed["example", {0.8, 0.1}]] //
DeleteCases[#, _Polygon, -1] &


-
Interesting. Thanks a lot! –  Yi Wang Apr 18 '14 at 11:40