3
$\begingroup$

Hi everyone and sorry for my english, I'm new to Mathematica. I have a problem combining points with histograms in Mathematica 12.0. I would like to display a frequency histogram along with points placed in the center of each bin. My ultimate goal is to put together a histogram with its Gaussian approximation. I tried using Show to display them together but the points are not centered in the bins and moreover the histogram is moved to the right. At this point, I would have another question to ask you: how can I create a Gaussian curve starting from Gaussian points (with ListLinePlot the graph is broken and I don't like it)? Here is the code of my attempt.

Thank you in advance.

 istomax900 ={{0.0246604, 4}, {0.0258038, 3}, {0.0269472, 6}, {0.0280907, 
  14}, {0.0292341, 21}, {0.0303775, 14}, {0.0315209, 12}, {0.0326643, 
  10}, {0.0338078, 11}, {0.0349512, 6}}
     gaussianamax900={{0.0246604, 1.57199}, {0.0258038, 3.60887}, {0.0269472, 
  6.88537}, {0.0280907, 10.9177}, {0.0292341, 14.3863}, {0.0303775, 
  15.7544}, {0.0315209, 14.338}, {0.0326643, 10.8445}, {0.0338078, 
  6.81618}, {0.0349512, 3.5606}}
    Histogram[WeightedData @@ Transpose[istomax900], Length[istomax900]]
    Show[ListPlot[gaussianamax900], 
     Histogram[WeightedData @@ Transpose[istomax900], Length[istomax900]]]
$\endgroup$
10
  • 1
    $\begingroup$ People are less likely to help you if you do not provide code that other people can easily copy and paste. Don't force other people to retype your code. $\endgroup$ May 16, 2020 at 11:29
  • $\begingroup$ ehm sorry , it's my first question. $\endgroup$ May 16, 2020 at 12:03
  • 1
    $\begingroup$ It's fine, that's why I'm teaching you how things are done here. Does Histogram[WeightedData @@ Transpose[istomax900], Length[istomax900], Epilog -> {Red, Point[gaussianamax900]}] do what you want? $\endgroup$ May 16, 2020 at 12:05
  • $\begingroup$ This allows me not to use "Show" but the points in the bins are not centered anyway $\endgroup$ May 16, 2020 at 12:17
  • $\begingroup$ By "centered", do you mean the points are centered on the bars, or centered on the tops of the bars? $\endgroup$ May 16, 2020 at 12:20

2 Answers 2

3
$\begingroup$

enter image description here

Here are a few options for you to start. Try to experiment by reading examples in documentation for listed below functions.

data=RandomVariate[NormalDistribution[1,3],10^3];

f1=NormalDistribution[1,3];
f2=FindDistribution[data];

options={Filling->None,PlotStyle->PointSize[.02]};

Show[Histogram[data,20,"ProbabilityDensity",PlotTheme->"Detailed"],
DiscretePlot[PDF[f1,x],{x,-10.5,10.5},#]&@@options]

Show[Histogram[data,20,"ProbabilityDensity",PlotTheme->"Detailed"],
DiscretePlot[PDF[f2,x],{x,-10.5,10.5},#]&@@options]

Show[Histogram[data,20,"ProbabilityDensity",PlotTheme->"Detailed"],
DiscretePlot[PDF[f2,x],{x,-10.5,10.5},#]&@@options,
Plot[PDF[f2,x],{x,-9,9},PlotStyle->Opacity[.3]]]

Show[Histogram[data,20,"ProbabilityDensity",PlotTheme->"Detailed"],
Plot[PDF[f2,x],{x,-9,9}]]
$\endgroup$
1
  • $\begingroup$ @kglr gave me the correct answer. Thank you for your availability $\endgroup$ May 16, 2020 at 12:49
4
$\begingroup$

Update: Using OP's data:

bincenters = istomax900[[All, 1]];
binwidth = First @ Differences[bincenters];
binspecs = {Min[#] - binwidth/2, Max[#] + binwidth/2, binwidth} & @ bincenters;

Show[Histogram[WeightedData @@ Transpose[istomax900], binspecs], 
 ListPlot[gaussianamax900, Joined -> True, PlotMarkers -> Automatic]]

enter image description here

Original answer:

SeedRandom[1]
data = RandomVariate[NormalDistribution[0, 1], 200]; 

histogram = Histogram[data]; 

You can extract the coordinates of bin centers and heights from histogram using Cases and use those points with ListLinePlot:

bincentersandheights = Cases[histogram , Rectangle[{xmin_, ymin_}, {xmax_, ymax_}, ___] :> 
  {Mean[{xmin, xmax}], ymax}, All];

Show[histogram, ListLinePlot[bincentersandheights,
  PlotStyle -> Directive[Thick, Red], PlotMarkers -> "●"]]

enter image description here

Alternatively, you can use bincentersandheights to construct the desired points and lines to be used as Epilog:

Show[histogram,  Epilog -> ({Thick, Red, Line@#, PointSize[Large], Red, Point@#} &@
   bincentersandheights)]

same picture

$\endgroup$
3
  • $\begingroup$ Perfect. You have fully satisfied my request. Thank you very match. $\endgroup$ May 16, 2020 at 12:51
  • $\begingroup$ @tommaso if kglr’s answer solves your problem, consider upvoting it and accepting it to reward their effort. $\endgroup$
    – MarcoB
    May 16, 2020 at 14:51
  • $\begingroup$ yes , sure. I click on the v, all right? $\endgroup$ May 16, 2020 at 15:06

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.