# ContourPlot: Different Colouring for Closed and Open Contours

I make a contour plot of a function as follows:

s = ContourPlot[(1/2) u^(2) + 1 - Cos[Theta], {Theta, -4 Pi, 4 Pi}, {u, -5, 5},
PlotLegends -> Automatic, PlotLabel -> "Contours of E"]


and I have to:

1) no shading between contours (I use ContourShadings ->None)
2) Closed contours are blue
3) Open contours are red
4) The contour dividing the regions of closed contours and open contours is a thick black line.

Anyone can help with 2) and 3)? I suppose at 4) I use ContourStyle-> {Thick,Black}?

Thanks

• The thin black line around the thick black one is the one that indicates the end of the closed contours? Normally the closed contours are from -2 to 2 but here it is from -2.07 to 2.07. – curious_math Oct 9 '15 at 23:47
• They should be at 2, as you suggest. – bbgodfrey Oct 10 '15 at 0:33
• I would gladly do, but it says i need 15 reputation to display my upvotes. – curious_math Oct 10 '15 at 1:14
• Indeed I found your answer most helpful, since it illuminated me, but again "thanks for the feedback! Once you earn a total of 15 reputaion, your votes will change the publicly displayed post score". – curious_math Oct 10 '15 at 1:20
• I am not sure that your comments, which appear to be meant for march, are reaching him. Unless your comments are attached to an answer that he authored, you need to include @march in the your comments to him. – bbgodfrey Oct 10 '15 at 2:14

This solves your specific problem. It does not generalize in the sense that you need to know the values of the contours beforehand and also know which ones correspond to closed and open. Anyway

vals = {0.5, 1, 2, 5, 6.5, 8, 10};
s = ContourPlot[(1/2) u^(2) + 1 - Cos[Theta]
, {Theta, -4 Pi, 4 Pi}, {u, -5, 5}
, Contours -> {0.5, 1, 2, 5, 6.5, 8, 10}
, ContourStyle -> Map[Which[# < 2, Blue, # == 2, {Black, Thick}, # > 2, Red] &, vals]
] Based on the now-deleted answer by belisarius is forth, I suggest

ContourPlot[(1/2) u^(2) + 1 - Cos[Theta], {Theta, -4 Pi, 4 Pi}, {u, -5, 5},
PlotLegends -> LineLegend[{Blue, Black, Red}, {"Closed", "Separatrix", "Open"}],
PlotLabel -> "Contours of E", ContourShading -> False, Contours -> Range[0, 15, .5],
ContourStyle -> Join[ConstantArray[Blue, 4], {{Thick, Black}}, ConstantArray[Red, 30]]] • @BrettChampion Thanks for correcting my careless use of PlotLegends. – bbgodfrey Oct 11 '15 at 13:21

When we don't know the levels corresponding to the open and closed contour lines, we can identify the closed contour lines exploiting the fact that for closed lines the first and last points are the same. Of course, there is no such convenient trick to identify a "Separatrix".

cp = ContourPlot[(1/2) u^(2) + 1 - Cos[Theta], {Theta, -4 Pi, 4 Pi}, {u, -5, 5},
ContourShading -> None, Contours -> {Automatic, 30}, ImageSize -> 300, PlotPoints ->200];

lines = Cases[Normal[cp], Line[x_], Infinity];
difs = Norm[#[[1, -1]] - #[[1, 1]]] & /@ lines;
closed = Pick[lines, difs, 0.];
open = Complement[lines, closed];
Row[{cp, Graphics[{Blue, closed, Red, open},
AspectRatio -> 1, ImageSize -> 300, Frame -> True]}] With

options = {ContourShading -> None, Contours -> {Automatic, 10}, ImageSize -> 300,
PlotPoints -> 200};
cp = ContourPlot[Cos[x] + Cos[y], {x, 0, 4 Pi}, {y, 0, 4 Pi}, Evaluate[options]];


the same procedure gives And, with

cp = ContourPlot[Evaluate[Sum[Sin[RandomReal[5, 2].{x, y}], {5}]],
{x, 0, 5}, {y, 0, 5}, Evaluate[options]]; 