10
$\begingroup$

When plotting contours with ContourPlot, one can use the option ContourStyle to change the contours of the plot.

With the instruction,

ContourPlot[f[x,y],{x,-1,1},{y,-1,1},ContourShading -> False, Contours -> Range[1,10], ContourStyle -> ColorData[10] /@ Range[10]]

I am able to change the colors of the contours, but they are thin.

On the other hand, if I use the instruction

ContourPlot[f[x,y],{x,-1,1},{y,-1,1},ContourShading -> False, Contours -> Range[1,10], ContourStyle -> Thick]

I am able to draw thicker contour lines, however, they are black.

I would like to draw the same contours simultaneously thicker and colored.

How could I proceed ?

Mathematica seems to indicate to use Directive[...], but I have not been able to use it...

$\endgroup$

4 Answers 4

10
$\begingroup$

You could use BaseStyle to set the Thick lines:

ContourPlot[x y, {x, -1, 1}, {y, -1, 1}, ContourShading -> False, 
 ContourStyle -> ColorData[10] /@ Range[10],
 BaseStyle -> Thick]

enter image description here

Alternatively you could Thread Directive over the colour list:

ContourStyle -> Thread @ Directive[Thick, ColorData[10] /@ Range[10]]

which will give the same result.

$\endgroup$
3
  • $\begingroup$ Beat me to it. +1 $\endgroup$
    – Mr.Wizard
    Commented Jul 15, 2014 at 12:19
  • $\begingroup$ This answers exactly my question. It is based on the fact that the same thickness of the lines is shared between all the contours. For my own curiosity, would you know how to adapt the thickness from one line to another, simultaneously with the colors ? (If it is too involved, don't bother of course !) $\endgroup$
    – jibe
    Commented Jul 15, 2014 at 12:21
  • $\begingroup$ @jibe See my answer for that variation. If you don't need that level of specificity I prefer BaseStyle. $\endgroup$
    – Mr.Wizard
    Commented Jul 15, 2014 at 12:23
8
$\begingroup$

A working way to use Directive:

ContourPlot[Cos[x] + Cos[y], {x, 0, 4 Pi}, {y, 0, 4 Pi},
 ContourShading -> False, 
 ContourStyle -> Array[Directive[Thick, ColorData[10]@#] &, 10]
]

enter image description here

Changing thickness along with color:

ContourPlot[Cos[x] + Cos[y], {x, 0, 4 Pi}, {y, 0, 4 Pi},
 ContourShading -> False, 
 ContourStyle -> Array[{AbsoluteThickness[#], ColorData[10]@#} &, 10]
]

enter image description here

The same result may be had using { } or Directive[ ] (for both variations).
Directive should be needed only when providing multiple style rules that are to apply to all lines, like this:

ContourPlot[Cos[x] + Cos[y], {x, 0, 4 Pi}, {y, 0, 4 Pi},
 ContourShading -> False, 
 ContourStyle -> Directive[AbsoluteThickness[5], Blue]
]

enter image description here

Whereas with { } you would get cyclic styling:

ContourPlot[Cos[x] + Cos[y], {x, 0, 4 Pi}, {y, 0, 4 Pi},
 ContourShading -> False, 
 ContourStyle -> {AbsoluteThickness[5], Blue}
]

enter image description here

$\endgroup$
8
  • 7
    $\begingroup$ I think my parents had that wallpaper in the 70s :-) $\endgroup$ Commented Jul 15, 2014 at 12:25
  • $\begingroup$ @SimonWoods LOL (really) $\endgroup$
    – Mr.Wizard
    Commented Jul 15, 2014 at 12:26
  • $\begingroup$ Unfortunately, I still don't have Mathematica 10, to get this new amazing wallpapers syntax ! ;) $\endgroup$
    – jibe
    Commented Jul 15, 2014 at 12:30
  • $\begingroup$ @jibe I'm not sure if that's a joke or if the second code doesn't work for you. If the latter, what version are you using? Please try this code from before I replaced Directive. $\endgroup$
    – Mr.Wizard
    Commented Jul 15, 2014 at 12:33
  • $\begingroup$ @Mr.Wizard Your two codes examples work perfectly well for me with Directive[] (which is what I had only tried.). Quite surprinsigly, I have Mathematica 9. and the version with {} instead of Directive also works (I just tried it.) $\endgroup$
    – jibe
    Commented Jul 15, 2014 at 12:40
1
$\begingroup$

One can also modify the resulting graphic directly:

original = ContourPlot[Cos[x] + Cos[y], {x, 0, 4 Pi}, {y, 0, 4 Pi}, 
   PlotLegends -> Automatic, Contours -> 10, ContourShading -> False, 
   ContourStyle -> ColorData["AvocadoColors"] /@ Rescale@Array[# &, 10], 
   PlotPoints -> 100];

original /. RGBColor[a__] :> Sequence[Thick, RGBColor@a]

enter image description here

The advantage of this method is, it also adjusts the thickness of lines in legend, while the solutions shown by Simon and Mr.Wizard doesn't, at least in v12.2.

$\endgroup$
1
  • 1
    $\begingroup$ I personally prefer using Directive[] instead of Sequence[] in a situation like this: original /. RGBColor[a__] :> Directive[Thick, RGBColor[a]] $\endgroup$ Commented Dec 25, 2020 at 7:16
0
$\begingroup$

Use Directive

ContourPlot[x+y,{x,-1,1},{y,-1,1},ContourShading->False,Contours->Range[1,10],ContourStyle->Directive[Thick,Red]]
$\endgroup$
1
  • 1
    $\begingroup$ Thank you, now all the contours are simultaneously thick and red. However, I would also like the colors to change from one contour to another, as in my first example. Would you know how to adapt your answer ? $\endgroup$
    – jibe
    Commented Jul 15, 2014 at 12:15

Your Answer

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

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