4
$\begingroup$

Goal

I would like to make a plot which satisfies the following:

  • Formost it should contain the Histogram of the data, with an overlay of the PDF
  • Two y-axes, the left being the count for the elements in the bin of the Histogram, the right being the Probability density for the PDF
  • Should show the "fit" of the PDF to the histogram of the data (by being at the same scale)

Code

data = RandomVariate[GumbelDistribution[], 1000];

While Histogram has an option to render the discretized PDF of the data, I rather see the smooth fitted version and the original.

Clearly this will use some combination of Show as to show the overlay for the discretized PDF and the smooth PDF one can type:

dist = SmoothKernelDistribution[data, "StandardGaussian"];
Show[Histogram[data, Automatic, "PDF"], 
 SmoothHistogram[data, Automatic, "PDF", PlotStyle -> Blue], 
 Plot[PDF[dist, x], {x, -7, 3}, PlotStyle -> Red]]

So how to get the regular Histogram[data] and the SmoothHistogram[data, Automatic, "PDF"] on the same plot, with the same kind of overlay we see above, but with their respective y-axes?

enter image description here

$\endgroup$
  • $\begingroup$ Adding a right y-axis with the default ticks and changing the ticks of the left y-axis to custom ticks, that represent the counts, would be the simplest way to achieve the desired output by just adding the proper options settings to your code. $\endgroup$ – Karsten 7. Oct 29 '16 at 16:56
  • $\begingroup$ @Karsten7. Except that doesn't handle ensuring that the PDF is shown at the same scale as the counts... $\endgroup$ – SumNeuron Oct 29 '16 at 16:59
  • $\begingroup$ @Karsten7. no? Counts are integers... PDF does not have to be, above I show the PDF option for Histogram, counts are much higher... Also, nope I do not recall posting this before. $\endgroup$ – SumNeuron Oct 29 '16 at 18:47
2
$\begingroup$

You may create FrameTicks on one axis by scaling a height to its corresponding PDF scale. I recommend specifying the Histogram bin width so that you know the parameters for the scaling. Also all three are plotting "PDF"

Below I use bin widths of 0.5 so I know what value to include in the scaling function.

SeedRandom[54321];
data = RandomVariate[GumbelDistribution[], 1000];

Show[
 Histogram[data, {1/2}, "PDF"],
 SmoothHistogram[data, Automatic, "PDF", PlotStyle -> Blue], 
 Plot[PDF[dist, x], {x, -7, 3}, PlotStyle -> Red],
 PlotRangeClipping -> True,
 GridLines -> {None, Automatic},
 Frame -> True,
 FrameTicks -> {
   {
    {Sequence @@ #[[1]], Sequence @@ ({#, "", {.005, 0}} & /@ Flatten@#[[2]])} &@
       MapAt[{#, # 1000/2} &, {1, All}]@FindDivisions[{0, .5}, {5, 5}],
    {Sequence @@ N@#[[1]], Sequence @@ ({#, "", {.005, 0}} & /@ Flatten@#[[2]])} &@
       FindDivisions[{0, .5}, {5, 5}]
    },
   {Automatic, Automatic}}]

enter image description here

Hope this helps.

$\endgroup$
  • $\begingroup$ that gets most of the way there, but the goal is to call Histogram[data] without the PDF specification. I am not quite sure what is going on with the FrameTicks but I think it is artificially making the left FrameTicks sequence? $\endgroup$ – SumNeuron Oct 30 '16 at 6:53
  • $\begingroup$ @SumNeuron The y-axis ticks are directly calculated on both sides. The chart is on the "PDF" scale and automatically places ticks on the left on that scale. Therefore, I override those ticks with scaled count ticks and then add the pdf ticks to the right side. You can make a function that takes the data and calculates the ticks for you based on it.. That way the calculation will not be hardcoded to a particular solution. $\endgroup$ – Edmund Oct 30 '16 at 12:14
  • $\begingroup$ that was the crux of the question... because using Show with the two different scales will not work. $\endgroup$ – SumNeuron Oct 30 '16 at 12:26

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.