# Is it possible to have the opacity as a function of the input variable in Plot3D?

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

## 1 Answer

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]] &)]


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.

• Nice, thanks a lot! If I were to use a color scheme such as "BlueGreenYellow", how would the syntax change?
– Pxx
Dec 9, 2021 at 16:44
• 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. Dec 9, 2021 at 16:51
• Plot3D[x + y, {x, 0, 10}, {y, 0, 10}, ColorFunctionScaling -> False, ColorFunction -> (Opacity[If[# < 8, 1, (10 - #)/2], ColorData["BlueGreenYellow"][Rescale[#, {0, 10}]]] &)] Dec 9, 2021 at 16:51
• 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]] &)] Dec 9, 2021 at 18:26