# ContourPlot of HistogramDistribution

I am trying to create a density plot of a 2-dimensional HistogramDistribution.

ContourPlot[
PDF[distribution, {x, y}],
{x, xrange[[1]], xrange[[2]]}, {y, yrange[[1]], yrange[[2]]},
PlotRange -> All
]


The result is however not as expected. It shows only two colors. Blue where the distribution is zero and White where it is non-zero.

How can I get a proper contour plot that shows the different discrete values of the distribution?

Your code does work as expected with e.g. a bivariate standard distribution:

ContourPlot[
PDF[BinormalDistribution[0.5], {x, y}],
{x, -3, 3}, {y, -3, 3},
PlotRange -> All, PlotPoints -> 50,
PlotLegends -> Automatic
]


Similarly,

data = RandomReal[{-10, 10}, {20, 2}];
ContourPlot[
PDF[HistogramDistribution[data], {x, y}],
{x, y} ∈ Rectangle[{-10, -10}, {10, 10}]
]


Not the prettiest of plots, but it works nonetheless.

So it is likely that the problem is in your distribution's definition, but you should include more information for us to give more informed advice.

• Your last plot is about what I would like to see. However I get only Blue and White patches. It seems that this depends only on the distribution. Possibly there are options which allow to influence the mapping between values and color? – highsciguy Apr 23 '16 at 13:10

If your distribution comes from some data, then the roughness of the distribution may cost you problems.

For example

data = RandomVariate[BinormalDistribution[.75], 10^5];
dist = HistogramDistribution[data]
ContourPlot[
Evaluate@PDF[dist, {x, y}], {x, -4, 4}, {y, -4, 4}]


You can change the bin size to make it smoother

dist = HistogramDistribution[data, 100];

ContourPlot[Evaluate@PDF[dist, {x, y}], {x, -4, 4}, {y, -4, 4},
Exclusions -> None]


But a better way is to use the SmoothDensityHistogram and it's friends to visualize the histogram

{SmoothDensityHistogram[data], SmoothHistogram3D[data]} // Row


• Your first plot is about what I see. However, I do not want to address the problem by smoothing the distribution. After all there is a reason why one makes a histogram. It is conceivable that Mathematica does not find contour lines if the distribution takes just a few discrete values but why does it fail to color the area. It just needs to evaluate the function and assign a color value according to it. – highsciguy Apr 23 '16 at 13:05
• @highsciguy If you post the data and plot you get, that's would be easier to find the reason. – xslittlegrass Apr 23 '16 at 15:33