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

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}]]

enter image description here

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]

enter image description here

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

(I am using Mathematica 9.0.1)

share|improve this question
add comment

4 Answers 4

up vote 1 down vote accepted

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},
   ContourShading->None
 ],
 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.

plot

share|improve this answer
add comment

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}, 
 ContourShading -> None, 
 Epilog -> Text["example", Scaled[{0.8, 0.1}]]]

enter image description here

---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]

enter image description here

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}, 
  ContourShading -> None], 
 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]]}]

enter image description here

share|improve this answer
    
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 at 19:15
    
I've added a bit but I don't know if I made it more or less clear :) –  gpap Apr 19 at 9:00
add comment

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.

enter image description here

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

share|improve this answer
add comment

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] &

enter image description here

share|improve this answer
    
Interesting. Thanks a lot! –  Yi Wang Apr 18 at 11:40
add comment

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.