Message Boards Message Boards

How do I set the range for Histogram[]?

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.

POSTED BY: Jay Gourley
4 Replies
Posted 3 years ago

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]

Plot of two histograms

POSTED BY: Jim Baldwin
Posted 3 years ago

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

enter image description here

If not, can you provide a specific example illustrating what you are looking for.

POSTED BY: Rohit Namjoshi
Posted 3 years ago
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}]

Two histograms with the same horizontal range.

POSTED BY: Jim Baldwin

Perfect, Jim. Thanks.

POSTED BY: Jay Gourley
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract