How to render level curves in several colors.

I have a function of two variables defined on a region D in the x-y plane and I am looking at the contour curves of the form f(x,y)=cnst for several different constants. I am using the command ContourPlot[…] to plot the function and it’s level curves and I would like to render the level curves in different colors. Alas, the function RGBColor[…] is not an option for ContourPlot[…]. Is there any other way to render the level curves in a chosen color? As an example, I have the following code.

a=0;b=1;c=0;d=2;
f[x_,y_]=Sin[Pi x]^2 Sin[Pi y]^2;

ContourPlot[f[x,y],{x,a,b},{y,c,d},
      ContourShading->False,
      AspectRatio->Automatic,
      Contours->{0,.2,.5,.7,.9},
      PlotPoints->100,
      AspectRatio->1,
      DisplayFunction->$DisplayFunction];

Consider using the Option ContourStyle:

Clear[f];
f[x_, y_] := Sin[Pi x]^2 Sin[Pi y]^2;

ContourPlot[
    f[x, y], 
    {x, 0,1}, {y, 0,2}, 
    ContourShading -> None, 
     AspectRatio -> Automatic, Contours -> {0, .2, .5, .7, .9}, 
     PlotPoints -> 100, AspectRatio -> 1, 
     ContourStyle -> {Red, Blue, Green, Yellow, Orange}
]

Adding the command "ContourStyle → {Red, Blue, Green, Yellow, Orange} " To my code results in a plot of the contour curves that are all orange colored. They are not {Red, Blue, Green, Yellow, Orange}
in that order.

Can I know the output? I mean I am interested in this, but I wanna see the output. :slight_smile:

(* Sorry about the mixup in the code the I posted. The cut and paste function somehow missed up the translation and left out a bunch of stuff. I think this post is correct. It shows my problem with the colors of the level curves. *)
a = 0; b = 1; c = 0; d = 2;
f[x_, y_] := Sin[Pi x]^2 Sin[Pi y]^2;

  p1 = ContourPlot[f[x, y], {x, a, 
          b}, {y, c, d}, ContourShading -> False, AspectRatio -> Automatic, 
  Contours -> {.7}, PlotPoints -> 100, ContourStyle -> RGBColor[1, 0, 1],
        AspectRatio -> 1];
  
  p2 = ContourPlot[f[x, y], {x, a, b}, {y, c, d}, ContourShading -> 
        False, AspectRatio -> Automatic, Contours -> {.9}, PlotPoints -> 
          100, ContourStyle -> RGBColor[1, 0, 1],
        AspectRatio -> 1];
  
  p3 = ContourPlot[f[x, 
        y], {x, 0, 1}, {y, 0, 2}, ContourShading -> None, AspectRatio -> 
  Automatic, Contours -> {0, .5, .7, .9}, PlotPoints -> 100,
      ColorFunction -> {RGBColor[0, 0, 0], RGBColor[1, 1, 0], RGBColor[
        0, 1, 0], GBColor[0, 0, 1]}, 
          ContourStyle -> {RGBColor[0, 0, 0], RGBColor[1,
           1, 0], RGBColor[0, 1, 0], GBColor[0, 0, 1]},
      AspectRatio -> 1]

(* Here is some code that shows my problem witn=h cotour colors. The cut and paste function did not work right in my revious post *)
a = 0; b = 1; c = 0; d = 2;
f[x_, y_] := Sin[Pi x]^2 Sin[Pi y]^2;
p1 = ContourPlot[f[x, y], {x, a, b}, {y, c, d}, ContourShading → False,
AspectRatio → Automatic, Contours → {.7},
PlotPoints → 100, ContourStyle → RGBColor[1, 0, 1],
AspectRatio → 1];
p2 = ContourPlot[f[x, y], {x, a, b}, {y, c, d}, ContourShading →
False, AspectRatio → Automatic, Contours → {.9}, PlotPoints →
100, ContourStyle → RGBColor[1, 0, 1],
AspectRatio → 1];
p3 = ContourPlot[f[x, y], {
x, 0, 1}, {y, 0, 2}, ContourShading → None, AspectRatio → Automatic,
Contours → {0, .5, .7, .9}, PlotPoints → 100, ContourStyle → {
RGBColor[0, 0, 0], RGBColor[1, 1, 0], RGBColor[0,
1, 0], GBColor[0, 0, 1]},
AspectRatio → 1]

Typo GBColor should be RGBColor. AspectRatio is specified twice, earlier one takes precedence.

ContourPlot[f[x, y], {x, 0, 1}, {y, 0, 2},
 ContourShading -> None,
 AspectRatio -> Automatic,
 Contours -> {0, .5, .7, .9},
 ContourStyle -> {RGBColor[0, 0, 0], RGBColor[1, 1, 0], RGBColor[0, 1, 0], RGBColor[0, 0, 1]}]

I see that you have succeded in doing what I want to do. But when I try to use your code on my machine it does not work correctly. I am attaaching my code as a file if you want to look at it I would be most greatful. Dave Barnes.

contours.nb (108 KB)

Hi David,

the code still exhibits the missing ‘R’ typo in one of the RGBColor heads.
Furthermore, you appear to be using Mathematica version 5.2.

We may be looking at a bug that’s 10 years or older.

It that’s so, the following code may be a work-around.

Show@MapThread[
ContourPlot[f[x, y], {x, a, b}, {y, c, d}, ContourShading → False,
AspectRatio → Automatic, Contours → {#1}, PlotPoints → 100,
ContourStyle → #2,
AspectRatio → 1] &,
{{0, .5, .7, .9}, {RGBColor[0, 0, 0], RGBColor[1, 1, 0],
RGBColor[0, 1, 0], RGBColor[0, 0, 1]}}
]