2
$\begingroup$

I don't know if I will be clear about what I am asking for, but here is my problem: how could I create a histogram (with the probability curve, later) when I have many different values (each one with frequency equals to one), in such a way that in the $x$ axis I put ranges (like: $0$ to $80$, $80$ to $160$, $160$ to $240$ and so on) and on the $y$ axis I have the frequency, that is the number of values that lie within the $x$ range?

To say, the list of my values is:

$200,540,563,1031,458,957,200,800,226,314,517,210,900,160,432,380,89,1300,850,120,350,222,243,600,300,173,549,80,500,162,500,450,2000,300,647,371$

I know I have to use BarChart. I just cannot get how to create a range on the $x$ axis and the frequency over $y$ axis.

Thank you!

$\endgroup$
1
  • 1
    $\begingroup$ Histogram[data, {80}] where data is the list of values? $\endgroup$ Apr 11, 2022 at 18:04

2 Answers 2

5
$\begingroup$

Get into the 21st century and use nonparametric density estimates. In other words, dump the histogram.

data = {200, 540, 563, 1031, 458, 957, 200, 800, 226, 314, 517, 210, 
   900, 160, 432, 380, 89, 1300, 850, 120, 350, 222, 243, 600, 300, 
   173, 549, 80, 500, 162, 500, 450, 2000, 300, 647, 371};
skd = SmoothKernelDistribution[data, "LeastSquaresCrossValidation",
  {"Bounded", {0, \[Infinity]}, "Gaussian"}];
Plot[PDF[skd, x], {x, 0, Max[data]}]

PDF

And get the CDF:

Plot[CDF[skd, x], {x, 0, Max[data]}]

CDF

$\endgroup$
1
  • $\begingroup$ Wondrous!! Thank you so much! $\endgroup$
    – Enrico M.
    Apr 13, 2022 at 10:21
4
$\begingroup$
data = {200, 540, 563, 1031, 458, 957, 200, 800, 226, 314, 517, 210, 
   900, 160, 432, 380, 89, 1300, 850, 120, 350, 222, 243, 600, 300, 
   173, 549, 80, 500, 162, 500, 450, 2000, 300, 647, 371};
bins = Table[80 i, {i, 0, Max[data]/80 + 1}]


Histogram[data, {bins}]

enter image description here

Histogram[data, {bins}, "Probability"]

enter image description here

$\endgroup$
4
  • $\begingroup$ Awesome! How to add the probability curve? $\endgroup$
    – Enrico M.
    Apr 11, 2022 at 18:10
  • 2
    $\begingroup$ Since the bins are of equal width Histogram[data, {80}] is sufficient. $\endgroup$ Apr 11, 2022 at 18:11
  • 1
    $\begingroup$ @xyzt You can count probabilities and these should sum up to one. probs = BinCounts[data, {bins}]/Length@data // N and Total@probs. $\endgroup$
    – Syed
    Apr 11, 2022 at 18:29
  • $\begingroup$ @RohitNamjoshi I was trying a new syntax variation for bspec. Thanks for the heads up. $\endgroup$
    – Syed
    Apr 11, 2022 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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