1
$\begingroup$

I'm a relative novice with Mathematica and could really use some help. I would like to get the heights of bars from a number of histograms, and thought that BinCounts would be a more efficient way to do this, as I will need to do it a number of times, and the data lists are quite large (I.e. 72000+ values between 0 and 1.2 ish). However, I am getting errors.

An example of how I generate my data set, and then a histogram is below:

SetDirectory["myfilepath"];
    
AllFiles = FileNames["*.csv"];
    
AllData = Import[#, {"Data", All, 6}] & /@ AllFiles;
    
Y = Flatten[AllData];

Histogram[{Y}, 50, LabelingFunction -> Above, ChartStyle -> ColorData["Crayola"]["SeaGreen"], AxesLabel -> {"Bins", "Height"}]

This all works fine. However when I try to use BinCounts as below, I get this error :BinCounts::dims: The dimension 72607 of the first argument is not the same as the number of binning specifications 1.

BinCounts[{Y},{50}]

If someone could help me to see where I have gone wrong, and how I can help to fix it, I would truly appreciate it.

$\endgroup$
1
  • $\begingroup$ Try: BinCounts[Y] $\endgroup$ Commented Aug 25, 2022 at 8:59

1 Answer 1

3
$\begingroup$

Look at the Documentation of BinCounts (press F1 when the cursor is over the command). You have multiple options, either just use

  • BinCounts[Y], which gives you bins in successive integer bins
  • BinCounts[Y, 50], gives bins of width 50.
  • BinCounts[Y, {xMin, xMax, dx}], gives bins of width dx between xMin and xMax.

But there is no version for BinCounts[Y, {50}], therefore the error.

To get the analog to your Histogram[Y, 50,...], with 50 automatically selected bins, you might want to use HistogramList which offers the same specifications as Histogram. However it returns two lists, one with the bin starts, and one with the bin counts. So if you are then only interested in the counts, use the second list. But you'd probably need both infos anyway. For example like:

{bins, counts}=HistogramList[Y, 50]

$\endgroup$
4
  • $\begingroup$ HistogramList is exactly what I needed thank you. $\endgroup$
    – Schaef
    Commented Aug 25, 2022 at 11:18
  • $\begingroup$ I did originally try BinCounts[Y,50] and it didn't work, it would only report {0,72607}, do you know what I was doing incorrectly? (Just for future use/my understanding) $\endgroup$
    – Schaef
    Commented Aug 25, 2022 at 11:20
  • $\begingroup$ You probably did nothing wrong. You just computed bins of width 50 starting at 0, of which obviously only one bin starting at 0 existed, containing all your 72607 data points. $\endgroup$
    – azt
    Commented Aug 27, 2022 at 21:53
  • $\begingroup$ PS: If you like the answer, please consider accepting the answer. $\endgroup$
    – azt
    Commented Aug 27, 2022 at 21:55

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.