Message Boards Message Boards

Histogram of data with standard deviation at 10^-4

Hello, I am in the process of writing a report and wanting to create a histogram of my records I turned to Mathematica. As it turns out Mathematica has a problem with displaying a normal distribution plot where sigma is small, around 5*10^-4.

diamhisto = Histogram[DataD, {0.009}]
{dbins, dcounts} = HistogramList[DataD, {0.009}]
dcenters = MovingAverage[dbins, 2]
dCurvData = Transpose[{dcenters, dcounts}]
Show[diamhisto, ListPlot[dCurvData], Plot[9*Exp[((\[Minus]18857.1428571)*((x - 23.99916667)^2))], {x, 23.75, 24.25}, PlotStyle -> Red]]

The contents of DataD is imported as follows: {24, 24, 24, 24, 24, 24, 24.01, 24, 23.99, 23.99}

I have been unable to find a reliable way of getting the plot to work.

Attachment

POSTED BY: Jakub Rasała
9 Replies
Posted 1 year ago

The issue is that making a histogram with just 3 unique numbers is just plain silly.

POSTED BY: Jim Baldwin

I tried plotting just the normal curve with mean set to 0. This is what Mathematica came up with. I don't think it's working right.

Attachment

Attachments:
POSTED BY: Jakub Rasała
sig = 5*10^-4;
mn = 162.7;

dat = RandomReal[NormalDistribution[mn, sig], 1000];

Table[
 (*function of normal distribution*)
 fun = PDF[NormalDistribution[u, s]][x];
 mn = Mean[dat];
 std = StandardDeviation[dat];

 {bin, cnt} = HistogramList[dat, {d std}, "PDF"];
 cent = MovingAverage[bin, 2];
 cDat = Transpose[{cent, cnt}];

 (*find fit using the points*)
 sol = fun /. FindFit[cDat, fun, {{u, mn}, {s, std}}, x];

 (*use build in function*)
 dist = PDF[EstimatedDistribution[dat, NormalDistribution[u, s]]][x];

 Show[
  h = Histogram[dat, {(d std)}, "PDF"],
  ListPlot[cDat],
  (*use the range of the histogram for your plot*)
  {min, max} = Options[h, PlotRange][[1, 2, 1]];
  Plot[{sol, dist}, {x, min, max}]
  , ImageSize -> 500]
 , {d, .1, .5, .1}]
POSTED BY: Martijn Froeling

This generates random data with normal distribution, no? I'm looking for a solution with data I aquired during my "study" I'm writing the report on.

POSTED BY: Jakub Rasała

Yes i understand, but i just generated some data with the same properties, the code will also work on your data, just replace the artificially generated data with your data.

It was more to demonstrate how you can automatically propagate plot ranges etc. the only input of my code is the bin width which is a percentage of the std.

By estimating all the ranges from the data the chance of "copy paste" / "type errors" is minimized. As you can see the mn and std i use to generate the data are not used anywhere else (although i only now see i reuse and overwrite the mn parameter).

POSTED BY: Martijn Froeling
DataD // MinMax
(* {162.76, 162.79} *)

which does not match the range in the histogram, one of them is incorrect.

Plot[9*Exp[((-18857.1428571)*((x - 23.99916667)^2))], {x, 23.75, 24.25},
  PlotStyle -> Red,
  PlotRange -> {{23.95, 24.05}, All},
  AspectRatio -> Full]
POSTED BY: Rohit Namjoshi

Oh my, I made a mistake. I was making multiple histograms and I copied over the wrong data. I guess that is what hapens when you make a post late in the evening. I edited it to the right values.

POSTED BY: Jakub Rasała

With the corrected value for DataD and using the Plot I posted in an earlier answer (which I think is correct)

Show[diamhisto, ListPlot[dCurvData], 
 Plot[9*Exp[((-18857.1428571)*((x - 23.99916667)^2))], {x, 23.75, 24.25},
  PlotStyle -> Red,
  PlotRange -> {{23.95, 24.05}, All},
  AspectRatio -> Full]]

I get this

enter image description here

POSTED BY: Rohit Namjoshi

I am beyond words both in how thankful and disappointed in myself I am. For some reason the PlotRange fixes everything. Thank you so much.

POSTED BY: Jakub Rasała
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract