3
$\begingroup$

It is possible to make a Plot3D of a function $f(x,y)$, and to have the plot having its opacity $\alpha$ decrease as $x$ increases? For example say the plot has a range $x \in (0,10)$, and the opacity should be $\alpha = 1$ (fully non-transparent) for $x \in (0,8)$ and $\alpha = \frac{10-x}{2}$ for $x \in (8,10)$ (fully transparent at $x=10$).

$\endgroup$

1 Answer 1

6
$\begingroup$

You can do this with ColorFunction:

Plot3D[x + y, {x, 0, 10}, {y, 0, 10}, ColorFunctionScaling -> False, 
 ColorFunction -> (Opacity[If[# < 8, 1, (10 - #)/2], 
     ColorData[97, 2]] &)]

enter image description here

A couple of things to point out:

  • this is setting ColorFunctionScaling -> False so that the formulas are in the form you specified them.

  • you won't get the normal surface colors if your color function is just Opacity, so I added the default color to the color function. One consequence it that you generally won't be able to do this with multiple functions in the same call to Plot3D.

$\endgroup$
4
  • $\begingroup$ Nice, thanks a lot! If I were to use a color scheme such as "BlueGreenYellow", how would the syntax change? $\endgroup$
    – Pxx
    Dec 9, 2021 at 16:44
  • 1
    $\begingroup$ Assuming coloring by height, Instead of ColorData[97, 2] one way would be to change ColorData["BlueGreenYellow", #3] inside the color function and change ColorFunctionScaling -> {False, False, True}. If you want the fade and color to use the same parameters (x/y/z) then you might need to do some manual rescaling one way or the other. $\endgroup$ Dec 9, 2021 at 16:51
  • $\begingroup$ Plot3D[x + y, {x, 0, 10}, {y, 0, 10}, ColorFunctionScaling -> False, ColorFunction -> (Opacity[If[# < 8, 1, (10 - #)/2], ColorData["BlueGreenYellow"][Rescale[#, {0, 10}]]] &)] $\endgroup$
    – Bob Hanlon
    Dec 9, 2021 at 16:51
  • 1
    $\begingroup$ A hack-ish alternative is to use Append[] on the color object resulting from e.g. ColorData["BlueGreenYellow"], which is effectively tacking on an extra alpha channel argument. Using Brett's example, Plot3D[x + y, {x, 0, 10}, {y, 0, 10}, ColorFunctionScaling -> False, ColorFunction -> (Append[ColorData[97, 2], If[# < 8, 1, (10 - #)/2]] &)] $\endgroup$ Dec 9, 2021 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.