2
$\begingroup$

I want to make a 3D plot where the ColorFunction is different depending on whether the function value is positive or negative. I know how to do the following:

 mycf[z_]:=Piecewise[{{Red,z<=0},{Blue,z>0}}]
 Plot3D[myfunc[x,y],...,ColorFunction->mycf,ColorFunctionScaling->False]

But what I really want is to have gradients rather than uniform colors.

I'd also like to use the full scale of the colors in both directions. So if the positive values are between 0 and 2 and the color scale is rainbow, I want 0 to be purple and 2 to be red; if the negative values only go to -0.5 and the color scale is grayscale, I want 0 to be black and -0.5 to be white.

$\endgroup$
3
$\begingroup$
   mycf[z_] := 
    Piecewise[{
               {ColorData["Rainbow"][-z/2], z <= 0}, 
               {ColorData["SouthwestColors"][z], z > 0}}]
   Plot3D[Sin[x] + Sin[y], {x, -Pi, Pi}, {y, -Pi, Pi}, 
       ColorFunction -> mycf, ColorFunctionScaling -> False]

enter image description here

$\endgroup$
0
$\begingroup$

So if the positive values are between 0 and 2 and the color scale is rainbow, I want 0 to be purple and 2 to be red; if the negative values only go to -0.5 and the color scale is grayscale, I want 0 to be black and -0.5 to be white.

Here is one way to go about it:

rc = ColorData["Rainbow", "BlendArgument"];
bw = Reverse[ColorData["GrayTones", "BlendArgument"]];

myGradient = With[{cl = 
     Join[Transpose[{Subdivide[-1/2, 0, Length[bw] - 1], bw}], 
          Transpose[{Subdivide[0, 1, Length[rc] - 1], rc}]]}, 
     Blend[cl, #] &];

Plot3D[2 Sin[x + Sin[y]]^2 - Sin[y - Sin[x]]/2, {x, -2 π, 2 π}, {y, -2 π, 2 π}, 
       ColorFunction -> (myGradient[#3] &), ColorFunctionScaling -> False, Mesh -> False]

some colored surface

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