TransWikia.com

Strange Show Behavior

Mathematica Asked by BOUNCE on January 16, 2021

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

2 Answers

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
]

Correct answer by Sjoerd Smit on January 16, 2021

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

Answered by Daniel Huber on January 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP