Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am plotting two histograms on the same graph:

g1 = Histogram[bc, ChartStyle -> {Red}]
g2 = Histogram[bcx]
Show[g1, g2, PlotRange -> {{0.1, 0.6}, All}]

enter image description here

But I would like the plots to overlap so that they are "see-through" and I can compare them. I've been trying with

ChartStyle -> {"Overlapped"}

which hasn't been working.

share|improve this question
add comment

4 Answers

up vote 14 down vote accepted

First method

You could make the one in front partially transparent:

bc = RandomVariate[NormalDistribution[], 1000];
bcx = RandomVariate[NormalDistribution[], 1000];

g1 = Histogram[bc, ChartStyle -> {Red}];
g2 = Histogram[bcx, ChartStyle -> {Directive[Blue, Opacity[.5]]}];
Show[g1, g2, PlotRange -> All]

Mathematica graphics

Direct method

The same effect can be achieved by plotting the two distributions in the same plot directly:

Histogram[{bc, bcx}, ChartStyle -> {Red, Blue}]
share|improve this answer
1  
Why lead with the more complex solution?! –  Brett Champion Feb 24 '12 at 21:54
1  
@BrettChampion the second one didn't occur to me until after I finished writing the more complex solution. –  Heike Feb 24 '12 at 21:59
add comment

Doesn't Histogram do this already?

data1 = RandomReal[NormalDistribution[0, 1], 500];
data2 = RandomReal[NormalDistribution[1, 1.5], 500];

Histogram[{data1, data2}]

Mathematica graphics


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

Mathematica graphics


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.

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]

Mathematica graphics

share|improve this answer
    
Is there a way to get rid of the middle lines? And get something like this on the second example. I like how they just set the Apperance to Transparent and its done. –  jmlopez Jun 4 '12 at 22:47
1  
I feel that it is a little absurd how there is tons of examples of how to use Histogram and yet there is no option to do what you just did in the update. All it really is is a line plot connecting points with the fill axis property. Anyway, thank you Mr. Wizard. –  jmlopez Jun 5 '12 at 7:46
add comment

You also have the option of SmoothHistogram.

data1 = RandomVariate[NormalDistribution[0, 1], 10^3];
data2 = RandomVariate[NormalDistribution[.4, 1], 10^3];

SmoothHistogram[{data1, data2}, 
 PlotStyle -> {{Thick, Red}, {Thick, Blue}}]

enter image description here

Or using one of the nonparametric distributions HistogramDistribution...

Plot[Evaluate[{PDF[HistogramDistribution[data1], x], 
   PDF[HistogramDistribution[data2], x]}], {x, -4, 4}, 
 Exclusions -> None, PlotPoints -> 100, 
 PlotStyle -> {{Thick, Red}, {Thick, Blue}}]

enter image description here

share|improve this answer
add comment

For g2 you should be able to change the definition to:

g1 = Histogram[bcx, ChartStyle -> Opacity[0.50, Blue]]

To get the front histogram to be more transparent. Fiddle around with the 0.50 to get it to look the way that you want.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.