I am plotting a continuous scalar function of three variables with this code:

function = (
  E^(-((2 (300 + 20 x + x^2 + 20 y + 2 y^2))/(
    4 + t^2))) (E^((2 (100 + 40 x + y^2))/(4 + t^2)) + E^((
     2 (100 + 40 y + y^2))/(4 + t^2)) + 
     2 E^((2 (20 x + (10 + y)^2))/(4 + t^2))
       Cos[(20 t (x - y))/(4 + t^2)]))/(π (4 + t^2));
DensityPlot3D[function, {x, -60, 60}, {y, -60, 60}, {t, 0, 60}, 
 ColorFunction -> "Rainbow",
 PlotLegends -> BarLegend["Rainbow"], PlotPoints -> 200]

enter image description here

The resulting plot has almost all of the dynamic range hidden below the dark blue blobs at the bottom, while I want to emphasize details of the light purple blobs in the middle and top. The transition between the blue and purple blobs also seems too sharp, and the gray bar alongside the bar legend seems to exclude most of the purple. Are there better ways to plot this function?

  • 1
    $\begingroup$ Please include the graphical output next time in cases like these, so people can think about a solution without having to run the code. Especially considering that the code takes a while to run, even on fairly powerful hardware $\endgroup$
    – Lukas Lang
    Sep 5, 2019 at 19:41
  • 1
    $\begingroup$ There are two good ways, I think. First, you can plot some contours using ContourPlot3D. That's how I visualize 3D wavefunctions. Second, you can use a non-linear OpacityFunction that allows for more definition in the blobs by cutting down on opacity. $\endgroup$
    – b3m2a1
    Sep 5, 2019 at 19:52
  • 1
    $\begingroup$ Playing around with your function, I think you'll have to transform the function to be plotted (plotting e.g. Log[function + 1*^-5] gives something a bit more reasonable already). The issue is that DensityPlot3D seems to sample very few colors in the interesting region, because all the values there only cover a tiny part of the range of all values $\endgroup$
    – Lukas Lang
    Sep 5, 2019 at 20:11
  • 1
    $\begingroup$ @LukasLang you can also supply a different non-linear ColorFunction $\endgroup$
    – b3m2a1
    Sep 5, 2019 at 21:26
  • 1
    $\begingroup$ @b3m2a1 I tried that, but I could never get any resolution in the lobes beyond a certain point. Checking the values supplied to ColorFunction (via Sow/Reap) indicates that the ColorFunction is just uniformly sampled ~300 times. So no matter how non-linear the ColorFunction is, it seems impossible to get more resolution than that... (unless I'm missing something) $\endgroup$
    – Lukas Lang
    Sep 5, 2019 at 21:32

1 Answer 1


By request, here's an example of how to use a non-linear opacity and color function for some fake wavefunction form (usually I use ListDensityPlot3D):

pol =
  Thread[{r, θ, φ} -> 
    CoordinateTransform["Cartesian" -> "Spherical", {x, y, z}][[1]]];
wfn = (E^(r/10))*Simplify[
     ExpToTrig@SphericalHarmonicY[2, 1, θ, φ] -
      ExpToTrig@SphericalHarmonicY[2, -1, θ, φ]
     ] /. pol;

 {x, -1, 1},
 {y, -1, 1},
 {z, -1, 1},
 OpacityFunction -> (Clip[#^3, {.2, 1}, {0, 1}] &),
 ColorFunction -> (Blend["AvocadoColors", #^3] &)

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.