4
$\begingroup$

I have searched the forum, I hope this isn't a duplicate question. I try to apply unique color for every contour line with the same elevation. In short, I want to create a MATLAB style contour plot. Consider this code

ContourPlot[Evaluate@Table[Cos[x] + Cos[y] == a, {a, -2, 2, 0.5}], 
{x, 0,  4 Pi}, {y, 0, 4 Pi}, PlotPoints -> 80]

enter image description here

I think this solution is lame. Is there a better solution for this?

$\endgroup$
  • $\begingroup$ this is related (and may be deemed a duplicate) but either way will be helpful: mathematica.stackexchange.com/q/86337/1997 $\endgroup$ – ubpdqn Jun 22 '15 at 7:40
  • $\begingroup$ I have read that post before. But I believe this is a different problem. $\endgroup$ – Deco Jun 22 '15 at 7:42
  • $\begingroup$ could you clarify what your issue is. Is it your desire to specify the colours or something else? $\endgroup$ – ubpdqn Jun 22 '15 at 7:46
  • $\begingroup$ If we invoke the option ContourShading -> None we will have contours with the same color. I want to color these contours so we can distinguish them by their elevations $\endgroup$ – Deco Jun 22 '15 at 7:52
8
$\begingroup$

Perhaps you might prefer this. Obviously you can change to your desired color function:

func[a_, b_, s_] := 
 With[{r = Range[a, b, s]}, 
  ContourPlot[Cos[x] + Cos[y], {x, 0, 4 Pi}, {y, 0, 4 Pi}, 
   Contours -> r, 
   ContourStyle -> 
    Table[{Thick, ColorData["Rainbow"][j/Length@r]}, {j, Length@r}], 
   ContourShading -> None]]

So, func[-2,2,0.5]

enter image description here

or func[-1,1,0.01]:

enter image description here

$\endgroup$
4
$\begingroup$

You can also style individual contours using the Contours setting that allows each contour level to have its own directive:

$ \{ \ldots, \{f_i ,g_i\}, \ldots \} \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ $ contour level $f_i$ drawn with graphics directive $g_i$

ContourPlot[Cos[x] + Cos[y], {x, 0, 4 Pi}, {y, 0, 4 Pi},
 ContourShading -> None,
 Contours -> ({#, Directive[Thickness[.015], ColorData[{"Rainbow", {-2, 2}}]@#]} & /@
   Range[-2, 2, 0.5])]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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