Doesn't Histogram
do this already?
data1 = RandomReal[NormalDistribution[0, 1], 500];
data2 = RandomReal[NormalDistribution[1, 1.5], 500];
Histogram[{data1, data2}]
Version 7 doesn't have HistogramDistribution
as shown by Andy Ross. Here is an alternative:
Histogram[{data1, data2},
BaseStyle -> FaceForm[None],
ChartStyle -> {EdgeForm[{Thick, Red}], EdgeForm[{Thick, Blue}]}
]
jmlopez asked for a method without the vertical lines. Here is one. The replacement may be a bit fragile. Andy's method is safer for version 8 users.
Update: modified to work in Mathematica 10 as well.
h =
Histogram[{data1, data2},
ChartStyle -> (Directive[#, AbsoluteThickness[3]] & /@ {Red, Blue}),
PerformanceGoal -> "Speed"
];
h2 =
Histogram[{data1, data2},
ChartStyle -> {{Red, Blue}, Directive[Opacity[0.1], EdgeForm[]]}
];
hline = h /. rec : {({{_Rectangle}} | {}) ..} :>
Line[ Flatten[rec, 2] /. _[{x_, y_}, {X_, Y_}, ___] :> Sequence[{x, Y}, {X, Y}] ];
Show[hline, h2]
kjo pointed out that my method fails when bars have a height of zero. The simplest fix I can think of is to avoid zero-height bars (which are not drawn, the source of the problem) by using a small offset value for the hspec function.
hfn = $MachineEpsilon + #2 &;
h = Histogram[{data1, data2}, {0.1}, hfn,
ChartStyle -> (Directive[#, AbsoluteThickness[3]] & /@ {Red, Blue}),
PerformanceGoal -> "Speed"]
h2 = Histogram[{data1, data2}, {0.1}, hfn,
ChartStyle -> {{Red, Blue}, Directive[Opacity[0.1], EdgeForm[]]}]
hline = h /.
rec : {({{_Rectangle}} | {}) ..} :>
Line[Flatten[rec, 2] /. _[{x_, y_}, {X_, Y_}, ___] :> Sequence[{x, Y}, {X, Y}]];
Show[hline, h2]