5
$\begingroup$

I've had the same effect in Mathematica 9 and 10.

I'm trying to color a 3D Plot with another function, let's call it colorFun ( it should highlight the areas where the colorFun is above a certain threshold), but ColorFunction seems to use the wrong coordinates.

Horribly colored minimal example

colorFun := Function[{x, y},If[x < y, Red, Blue]]
Plot3D[Evaluate[x^2+y^2],{x,0,1},{y,0,2},ColorFunction->colorFun]

Holy cow it's ugly!

Note that x and y have different intervals plotted, so the divide should not be through the middle. Similar things happen if you change the colorFun to something like y<0.5 . It seems that the ColorFunction is not using the same coordinates as the function, but rather a kind of normalized version, always going from 0 to 1.

Is this a bug, or is Mathematica beating my ability to understand computers again?

$\endgroup$
  • 1
    $\begingroup$ You actually essentially answered your own questions: the values passed to ColorFunction are in fact scaled to [0,1] by default. To avoid that, use ColorFunctionScaling -> False. $\endgroup$ – MarcoB Jul 15 '15 at 14:57
  • $\begingroup$ Possible duplicates: (6741), (6986), (14758) $\endgroup$ – Mr.Wizard Jul 15 '15 at 15:05
  • 1
    $\begingroup$ Please pardon the reopen but I was just about to post an answer when it closed. If you would still like to close this as a duplicate let me know. Either way I hope my answer is useful to you. $\endgroup$ – Mr.Wizard Jul 15 '15 at 15:20
  • $\begingroup$ I'm fine with keeping it open, if others agree to my impression that your answer help show a new facet of the problem, as compared to the old answers. $\endgroup$ – GeckoGeorge Jul 15 '15 at 15:36
5
$\begingroup$

You'll get a much crisper output if you use Mesh functionality instead:

Plot3D[x^2 + y^2, {x, 0, 1}, {y, 0, 2},
  MeshFunctions -> {#/#2 &},
  Mesh -> {{1}}, 
  MeshShading -> {Red, Blue}
]

enter image description here

Or with additional grid lines:

Plot3D[x^2 + y^2, {x, 0, 1}, {y, 0, 2},
  MeshFunctions -> {#/#2 &, # &, #2 &}, 
  Mesh -> {{1}, 12, 12},
  MeshShading -> {{{Red, Blue}}}
]

enter image description here

$\endgroup$
  • $\begingroup$ It does look a lot better. Thanks for teaching cool new stuff! $\endgroup$ – GeckoGeorge Jul 15 '15 at 15:34
4
$\begingroup$

And immediately after submitting, I find the answer here:

Using ColorFunctionScaling->False

Plot3D[Evaluate[x^2+y^2],{x,0,1},{y,0,2},ColorFunction->colorFun,ColorFunctionScaling->False]

Gives the correct coloring. Sorry to bother you all, and thanks for listening!

Decided to keep the question and answer it myself for other who might do the same searches I did, since finding the answer was kinda random for me.

$\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.