I want to overlay two objects of SmoothDensityHistogram using Show, in order to highlight a region of the shared plot space. However, the top object completely obscures the bottom one. I tried to set an opacity for the objects, but SmoothDenstiyHistogram does not accept PlotStyle as an argument to pass either Opacity[...] or Directive[Opacity[...]]. Is there a way to make this work?

  • 2
    $\begingroup$ Could you post example code of your histograms? $\endgroup$
    – Carl Lange
    Jan 16, 2019 at 20:15
  • $\begingroup$ @CarlLange My histograms are generated from large data files, but any two sets of random data passed into a SmoothDensityHistogram and overlaid will show the issue. $\endgroup$
    – avikarto
    Jan 16, 2019 at 20:17

1 Answer 1


You can do this by setting the ColorFunction to use Opacity.

Let's define our ColorFunction, which basically just returns a color for a given plot value:

cf[z_] := Opacity[z, RGBColor[z, 1 - z, 1]]

(You can also wrap any of the ColorData functions with that Opacity and it should work)

Now we can use that to show two plots together:

s1 = SmoothDensityHistogram[
  RandomVariate[BinormalDistribution[.2], 10], PlotRange -> {-3, 3}, 
  Frame -> None, ColorFunction -> cf]
s2 = SmoothDensityHistogram[
  RandomVariate[BinormalDistribution[.5], 10], PlotRange -> {-3, 3}, 
  Frame -> None, ColorFunction -> cf]

Overlay[{s1, s2}]

enter image description here

  • $\begingroup$ Perfect, thanks! $\endgroup$
    – avikarto
    Jan 16, 2019 at 20:31
  • $\begingroup$ Do you not want to have something like ColorFunctionScaling -> False? Otherwise, the highest values have the same color even if there's a large difference in the "peakedness" of the two densities? $\endgroup$
    – JimB
    Jan 17, 2019 at 4:35
  • $\begingroup$ @JimB That's a good point - probably a good idea to add your own answer working around that issue. $\endgroup$
    – Carl Lange
    Jan 17, 2019 at 9:03

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.