1
$\begingroup$

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?

$\endgroup$
2
$\begingroup$

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
]

Mathematica graphics


Similarly,

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

Mathematica graphics

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.

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – highsciguy Apr 23 '16 at 13:10
1
$\begingroup$

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

enter image description here

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]

enter image description here

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

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

enter image description here

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – highsciguy Apr 23 '16 at 13:05
  • $\begingroup$ @highsciguy If you post the data and plot you get, that's would be easier to find the reason. $\endgroup$ – xslittlegrass Apr 23 '16 at 15:33

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.