2
$\begingroup$

I'd like to plot two histograms on top of one another, but they have very different scales. Is it possible to plot a histogram with two y-axes, similar to the custom function found here for plots?

$\endgroup$
  • $\begingroup$ You can also plot a pdf Histogram[{d1, d2}, Automatic, "PDF"] but then you have to handle FrameTicks. $\endgroup$ – Kuba Mar 29 '15 at 22:37
5
$\begingroup$

More or less a duplicate of this answer, but just to demonstrate how it is used:

Some data:

d1 = RandomVariate[NormalDistribution[0, 1], 200];
d2 =  RandomVariate[NormalDistribution[0, 1], 1000];

Create the individual plots and then combine them using Overlay

optsall = {Axes -> False, Frame -> True, ImageSize -> 600, 
   BaseStyle -> {Thick, FontSize -> 14}};
opts1 = {ImagePadding -> {{Scaled[0.05], Scaled[0.11]}, {Scaled[0.04],
       Scaled[0.02]}}, 
   FrameStyle -> {Thick, {Thick, Red}, Thick, Opacity[0]}, 
   FrameLabel -> {"x", "First y-label"}, 
   ChartStyle -> Directive[Opacity[0.5], Red]};
opts2 = {ImagePadding -> {{Scaled[0.05], Scaled[0.11]}, {Scaled[0.04],
       Scaled[0.02]}}, 
   FrameStyle -> {Opacity[0], Opacity@0, Opacity@0, Blue}, 
   FrameTicks -> All, 
   FrameLabel -> ConstantArray["Second y-label", 4], 
   ChartStyle -> Blue};
Histogram[d1, Join[optsall, opts1]];
Histogram[d2, Join[optsall, opts2]];
Overlay[{%, %%}]

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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