# How to apply different colors to different contours

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]


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

• this is related (and may be deemed a duplicate) but either way will be helpful: mathematica.stackexchange.com/q/86337/1997 – ubpdqn Jun 22 '15 at 7:40
• I have read that post before. But I believe this is a different problem. – Deco Jun 22 '15 at 7:42
• could you clarify what your issue is. Is it your desire to specify the colours or something else? – ubpdqn Jun 22 '15 at 7:46
• 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 – Deco Jun 22 '15 at 7:52

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


So, func[-2,2,0.5]

or func[-1,1,0.01]:

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