1
$\begingroup$

I have generated many random variables within a specific function, and have counted them up. So far, I have counted the variables:

 bcount1=BinCounts[function,{0,20,1}]
 {86,10,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}

Now I need to generate a histogram with a bin width of 1 that'll display something like:

x-axis 0 to 1 ---> a bar 86 units high x-axis 1 to 2 ---> a bar 10 units high x-axis 2 to 3 --> a bar 2 units high

and so on and so forth....however the Histogram function is giving me the reverse. It's telling me something from 80 to 100 is a bar 1 unit high..

Can I get some help with appropriate syntax?

$\endgroup$
2
$\begingroup$
bincounts = {86, 10, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
bins = Range[0, 20, 1];

You can use BarChart:

BarChart[bincounts]

Or Histogram after reconstructing an input data set using bins and bincounts:

Histogram[Flatten[ConstantArray[##] & @@@ Transpose[{Most@bins, bincounts}]]]
$\endgroup$
1
$\begingroup$
data = {86, 10, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
BarChart[data, ChartLabels -> Range[20]] 

enter image description here

If you must have a true histogram:

Histogram[Flatten[Table[i, {i, Length[data]}, {j, data[[i]]}]]]

enter image description here

$\endgroup$
  • $\begingroup$ Yeah, but I still have a counter for "86" through a bar that is 1 unit in height. I want a bar 86 units in height to represent the number of data sets between 0 and 1 that were generated from my function. I had a previous function that generated random variables for many simulations, and I'm counting how many of those simulated variables fall within a range. I just need a histogram that has 0 to 1 with a height of 86, 1 to 2 with a height of 10, 2 to 3 with a height of 2, 3 to 4 with a height of 0, etc. etc $\endgroup$ – user12289 Feb 5 '15 at 19:00
  • $\begingroup$ David, care to edit your post ? $\endgroup$ – Sektor Feb 5 '15 at 20:33
0
$\begingroup$
values = Range[0, 20];
frequencies = {86, 10, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
n = Length[values]; (* 21 *)
data = Flatten@Array[Table[values[[#]], {frequencies[[#]]}] &, n];

Histogram[data, {First@values, Last@values, 1}]

Mathematica graphics

$\endgroup$

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.