2
$\begingroup$

I'm looking for a way to get two smooth histogram curves to have the same max height. The plots are usually scaled by area or something, however, the only way I've been able to find that works is a really bad workaround. Anyone know of a better solution?

Original SmoothHistogram:

data = {RandomVariate[NormalDistribution[10, 2], 10000], RandomVariate[NormalDistribution[10, 0.7], 25000]^10/10^9}; SmoothHistogram[data, PlotStyle -> {Blue, Red}] Normal SmoothHistogram is scaled wrong

Bad Workaround:

Show[SmoothHistogram[data[[1]], Automatic, "Intensity", PlotRange -> {{0, 100}, All}, PlotStyle -> Blue], SmoothHistogram[data[[2]], Automatic, "Intensity", PlotRange -> All, PlotStyle -> Red] /. Line[x_] :> Line[{#[[1]], 2.5 #[[2]]} & /@ x]] Scaled SmoothHistogram needs to be manually changed

$\endgroup$
1
  • 1
    $\begingroup$ "The plots are usually scaled by area or something" ... Probability density functions integrate to 1. Using the maximum has no theoretical foundation. But you can always compute a SmoothKernelDistribution and plot it any way you like. $\endgroup$
    – Szabolcs
    May 11 '18 at 12:04
3
$\begingroup$

Plotting the PDFs of SmoothKernelDistributions using two vertical axes with different scales:

ClearAll[x]
skds = SmoothKernelDistribution /@ data;
maxvalues = NMaxValue[{PDF[#, x], 0 <= x <= 50}, x] & /@ skds
Plot[{PDF[skds[[1]], x], PDF[skds[[2]], x] (Divide @@ maxvalues)}, {x, 0, 50}, 
 PlotRange -> All, Frame -> True, Filling -> Axis,
 FrameLabel -> {{Style["PDF1", Blue], Style["PDF2", Red]}, {"x",  None}}, 
 FrameStyle -> {{Blue, Red}, { Black, Black}}, 
 PlotStyle -> {Blue, Red}, 
 FrameTicks -> {{Automatic, 
    Charting`FindTicks[{0, maxvalues[[1]]}, {0, maxvalues[[2]]}][##] &}, 
  {Automatic, Automatic}}]

enter image description here

Update: An alternative way to get maxvalues:

 maxvalues2  =  Max @ #["PDFValues"] & /@ skds
 {0.197102, 0.0681705}
$\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.