I'm trying to represent the absolute density of a 2-variable scalar function by color and tranparency/opacity. By absolute denstiy, I mean the absolute function value. I tried to do the coloring as follows, but don't know how to give one color for positive values and the other for negative values. The following plot has automatically scaled color and looks okay, only because the function is symmetrical.

 Cos[x] + Cos[y], {x, -2 Pi, 2 Pi}, {y, -2 Pi, 2 Pi},
 ColorFunction -> (Blend[{Blue, Yellow}, LogisticSigmoid[#]] &), 
 ColorFunctionScaling -> False

enter image description here

What I want to do further is to add opacity based on the absolute value. When the density function has low absolute value (whether positve or negative) in some region, then the region must be transparent relatively more than regions that have higer absolute value. And completely transparent when absolute value is 0. I tried using OpacityFunction but that didn't work.

I think one could color positive and negative regions with dintinct colors (monotonically, without concerning values), then add absolute opacity. That will consequently make regions with lower value less intense. Ultimately, I want to Blend such plots.


1 Answer 1


You can use the two argument form of Opacity:

DensityPlot[Cos[x] + Cos[y], {x, -2 Pi, 2 Pi}, {y, -2 Pi, 2 Pi}, 
 ColorFunction -> (Opacity[Abs[#], Blend[{Blue, Yellow}, LogisticSigmoid[#]]] &), 
 ColorFunctionScaling -> False, PlotPoints -> 100]

enter image description here


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.