5
$\begingroup$

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

enter image description here

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:

enter image description here

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

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

3 Answers 3

7
$\begingroup$

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]

enter image description here

$\endgroup$
1
  • $\begingroup$ +1 You were a little faster than me, it seems... $\endgroup$
    – Jens
    Jul 16, 2013 at 6:19
5
$\begingroup$

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.

$\endgroup$
3
$\begingroup$

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 getenter image description here

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

yellow.white.png

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

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

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.