# Overlaying two SmoothDensityHistograms

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?

• Could you post example code of your histograms? Jan 16, 2019 at 20:15
• @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. Jan 16, 2019 at 20:17

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}]


• Perfect, thanks! Jan 16, 2019 at 20:31
• 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?
– JimB
Jan 17, 2019 at 4:35
• @JimB That's a good point - probably a good idea to add your own answer working around that issue. Jan 17, 2019 at 9:03