2
$\begingroup$

When attempting to overlay a Discrete Plot on a Histogram, no matter the order I pass each argument in Show, the Histogram stacks over the Discrete Plot. Is it possible to lay the DiscretePlot over the histogram instead? enter image description here

$\endgroup$
1
  • 2
    $\begingroup$ it would be much easier for us if you could post the code instead of the snapshot $\endgroup$
    – MMA13
    Oct 7, 2020 at 8:22

2 Answers 2

3
$\begingroup$

If you want to show a histogram and compare it against a PDF, you need to specify the "Probability" (for discrete distributions) or "PDF" (for continuous ones) property to fit the two on top of each other. Otherwise the axes are going to disagree and one plot will swamp out the other:

data = RandomVariate[GeometricDistribution[1/2], 100];
Show[
 Histogram[data, Automatic, "Probability"],
 DiscretePlot[PDF[GeometricDistribution[1/2], x], {x, 0, Max[data]}],
 PlotRange -> All
]

For continuous data:

data = RandomVariate[ExponentialDistribution[1/2], 100];
Show[
 Histogram[data, Automatic, "PDF"],
 Plot[PDF[ExponentialDistribution[1/2], x], {x, 0, Max[data]}],
 PlotRange -> All
]
$\endgroup$
1
$\begingroup$

In Show, Plot options that are given later will override those given earlier. So, make sure your axes fit or do not draw them. There is another possibility. Overlay will simply draw one picture over another.

Here is an example:

d1 = RandomReal[{0, 1}, 100];
g1 = Histogram[d1]
g2 = Plot[30 Sin[2 Pi x], {x, 0, 1}]
Show[{g1, g2}]
Overlay[{g1, g2}]

enter image description here

$\endgroup$
1
  • 2
    $\begingroup$ Overlay will almost never properly align the axes, though. $\endgroup$ Oct 7, 2020 at 10:22

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.