I want to create multiple histograms of different data sets but with the same range for comparison. How do I do it? I want the binning and height scales to be automatic for the each set. But I want the range to be uniform among the histograms.
Finding the joint range of values is one way with adding in a little flexibility.
SeedRandom[12345];
data1 = RandomVariate[NormalDistribution[0, 1], 100];
data2 = RandomVariate[NormalDistribution[5, 3], 200];
(* Get minimum and maximum of all datasets combined *)
{xmin, xmax} = MinMax[Join[data1, data2]];
(* Allow for some flexibility *)
xlow = xmin - 0.1 (xmax - xmin);
xhigh = xmax + 0.1 (xmax - xmin);
Histogram[data1, Automatic, PlotRange -> {{xlow, xhigh}, Automatic}]
Histogram[data2, Automatic, PlotRange -> {{xlow, xhigh}, Automatic}]
Hi Jay,
Do you mean something like this?
d1 = RandomVariate[NormalDistribution[0, 1], 200];
d2 = RandomVariate[NormalDistribution[4, 1], 200];
d3 = RandomVariate[NormalDistribution[-4, 1], 200];
Histogram[{d1, d2, d3}]
If not, can you provide a specific example illustrating what you are looking for.
An alternative to the Histogram that would elevate you to the 21st century is the nonparametric density estimate. Two advantages is that you wouldn’t have to do any calculations to determine the overall range and each can be viewed without obstruction when placed on the same figure (unlike overlapping histograms).
SeedRandom[12345];
data1 = RandomVariate[NormalDistribution[0, 1], 100];
data2 = RandomVariate[NormalDistribution[5, 3], 200];
Show[SmoothHistogram[data1, Automatic, "PDF", PlotStyle -> Red],
SmoothHistogram[data2, Automatic, "PDF", PlotStyle -> Green,
PlotLegends -> LineLegend[{Red, Green}, {"data1", "data2"}]],
PlotRange -> All]
Perfect, Jim. Thanks.


