ContourPlot and Colorfunction

I have the following code:

ContourPlot[
(2.7 (-3.8099999999999996 + 7.24 x - 3.24 x^2 + 3.62 y - 6.48 x y + 6.48 x^2 y))/
( 11.43 - 6.48 x + 3.24 x^2 - 3.24 y + 6.48 x y) -
( 3 (-(-1 + x)^2 + (1 + 2 (-1 + x) x) y))/( 3 + (-2 + x) x + (-1 + 2 x) y) ,
{x, 0.5, 1}, {y, 0, 0.5},
ContourStyle -> {Black, Thick}, Contours -> {0},
ColorFunction ->
(If[#1 > 0, Directive[Lighter[Lighter[Lighter[Yellow]]]], White] &)]


which gives me

But my color function should color the area above the black line white while color area below the black line yellow.

However, if I use the colorfunction Rainbow, then it will give me the correct coloring result:

What should I do to make my 1st color function behave as expected?

• Your code gives what you expected when I run it on V9.0.1 on OS X 10.6.8. – m_goldberg Jul 16 '13 at 4:25
• It also works as expected on V8.0.4 on OS X 10.7.5. What platform is your problem arising on? – Jens Jul 16 '13 at 5:08
• weird... mine is V9.0.1 on OS X 10.8.4 – wdg Jul 16 '13 at 5:17
• @m_goldberg,@Jens, please try the updated code. Something was lost in the earlier code. – wdg Jul 16 '13 at 5:57

I always forget about ColorFunctionScaling too:

ContourPlot[(2.7 (-3.8099999999999996 + 7.24 x - 3.24 x^2 + 3.62 y - 6.48 x y +
6.48 x^2 y))/(11.43 - 6.48 x + 3.24 x^2 - 3.24 y + 6.48 x y) - (
3 (-(-1 + x)^2 + (1 + 2 (-1 + x) x) y))/(3 + (-2 + x) x + (-1 + 2 x) y),
{x, 0.5, 1}, {y, 0, 0.5},
ContourStyle -> {Black, Thick}, Contours -> {0},
ColorFunction -> (If[#1>0, Directive[Lighter[Lighter[Lighter[Yellow]]]], White] &),
ColorFunctionScaling -> False]


• +1 You were a little faster than me, it seems... – Jens Jul 16 '13 at 6:19

The argument to the ColorFunction shouldn't be scaled, because if it is then the zero passed to the ColorFunction depends on whether the minimum and maximum of the plot values are symmetric with respect to zero or not.

So we have to add the option ColorFunctionScaling -> False to the ContourPlot as follows:

ContourPlot[(2.7 (-3.8099999999999996 + 7.24 x - 3.24 x^2 +
3.62 y - 6.48 x y + 6.48 x^2 y))/(11.43 - 6.48 x +
3.24 x^2 - 3.24 y +
6.48 x y) - (3 (-(-1 + x)^2 + (1 + 2 (-1 + x) x) y))/(3 + (-2 +
x) x + (-1 + 2 x) y), {x, 0.5, 1}, {y, 0, 0.5},
ContourStyle -> {Black, Thick}, Contours -> {0},
ColorFunction -> (If[#1 > 0,
Directive[Lighter[Lighter[Lighter[Yellow]]]], White] &),
ColorFunctionScaling -> False]


This produces the correct result.

This is more of very long comment than an answer, but I hope it cast more murk on the problem.

If I evaluate

ContourPlot[
(2.7 (-3.8099999999999996 + 7.24 x - 3.24 x^2 + 3.62 y - 6.48 x y + 6.48 x^2 y))/
(11.43 - 6.48 x + 3.24 x^2 - 3.24 y + 6.48 x y) -
(3 (-(-1 + x)^2 + (1 + 2 (-1 + x) x) y))/(3 + (-2 + x) x + (-1 + 2 x) y),
{x, 0.5, 1}, {y, 0, 0.5},
ContourStyle -> {Black, Thick},
Contours -> {0},
ColorFunction -> (If[#1 > 0, Directive[Lighter[Lighter[Lighter[Yellow]]]], White] &)]


I get

all.yellow.png

However, If I change -3.8099999999999996 to -3.81 and only that and then evaluate

ContourPlot[
(2.7 (-3.81 + 7.24 x - 3.24 x^2 + 3.62 y - 6.48 x y + 6.48 x^2 y))/
(11.43 - 6.48 x + 3.24 x^2 - 3.24 y + 6.48 x y) -
(3 (-(-1 + x)^2 + (1 + 2 (-1 + x) x) y))/(3 + (-2 + x) x + (-1 + 2 x) y),
{x, 0.5, 1}, {y, 0, 0.5},
ContourStyle -> {Black, Thick},
Contours -> {0},
ColorFunction -> (If[#1 > 0, Directive[Lighter[Lighter[Lighter[Yellow]]]], White] &)]


I get

So both of the previously posted answers may be right, but they don't seem to tell the whole story.

• Nice observation. What leads to this difference? – wdg Jul 17 '13 at 9:17
• @wdg. I'd like to know the answer to that myself. It seems very strange behavior on the part of Mathematica. – m_goldberg Jul 17 '13 at 13:24
• I tried to compare evaluation trace but my laptop response was an attempt to fly away. Interesting observation :) +1. – Kuba Jul 19 '13 at 7:25