3
$\begingroup$

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

$\endgroup$
6
  • $\begingroup$ 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. $\endgroup$ Oct 9, 2015 at 23:47
  • $\begingroup$ They should be at 2, as you suggest. $\endgroup$
    – bbgodfrey
    Oct 10, 2015 at 0:33
  • $\begingroup$ I would gladly do, but it says i need 15 reputation to display my upvotes. $\endgroup$ Oct 10, 2015 at 1:14
  • $\begingroup$ 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". $\endgroup$ Oct 10, 2015 at 1:20
  • $\begingroup$ 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. $\endgroup$
    – bbgodfrey
    Oct 10, 2015 at 2:14

3 Answers 3

5
$\begingroup$

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}
  , ContourShading -> None
  , Contours -> {0.5, 1, 2, 5, 6.5, 8, 10}
  , ContourStyle -> Map[Which[# < 2, Blue, # == 2, {Black, Thick}, # > 2, Red] &, vals]
 ]

enter image description here

$\endgroup$
6
$\begingroup$

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

enter image description here

$\endgroup$
1
  • $\begingroup$ @BrettChampion Thanks for correcting my careless use of PlotLegends. $\endgroup$
    – bbgodfrey
    Oct 11, 2015 at 13:21
0
$\begingroup$

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

enter image description here

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

enter image description here

And, with

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

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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