2
$\begingroup$

What is the easiest way to create an histogram with the heights of each bin already determined? I read all the support page on histograms but I don't understand if there is a way to create an histogram simply having a list of bins and a list of heights for heach bin. Does anyone have suggestion?

$\endgroup$

closed as off-topic by Dr. belisarius, MarcoB, Jens, user9660, Kuba Feb 9 '16 at 9:18

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Dr. belisarius, MarcoB, Jens, Community, Kuba
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    $\begingroup$ Maybe BarChart. Or you could just use ListPlot. $\endgroup$ – march Feb 8 '16 at 22:45
  • 3
    $\begingroup$ data = Table[RandomReal[10], {100}]; BarChart[data] $\endgroup$ – David G. Stork Feb 8 '16 at 22:47
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Michael E2 Feb 8 '16 at 23:21
4
$\begingroup$

Suppose your data is in the form:

myData = {{3, 6}, {4, 9}, {6, 10}};

meaning you want a histogram bar at position 3 of height 6, a bar at 4 of height 9, and so on.

Then to get a true histogram, you can do this:

Histogram[Flatten[Table[#[[1]], #[[2]]] & /@ myData], 10]

or

Histogram[Flatten[Table @@@ myData], 10]

enter image description here

Having a true histogram makes it a bit easier to change the width of bins (Histogram[Flatten[Table[#[[1]], #[[2]]] & /@ myData], 5]).

$\endgroup$
2
$\begingroup$

It depends on what you want.

A hack is to generate data according to your bins.

myData = {{1, 15}, {3, 6}, {4, 9}, {6, 10}};
Histogram[Flatten[ConstantArray[#[[1]], #[[2]]]& /@ myData], {1, 10, 1}]

Otherwise you can also be more explicit an exploit the fact the the Histogram function allows you to specify functions for both the bins and the counts. However it is important to understand that your bins have the meaning of: # of items in interval [min,max). Let's say we have our data as:

myData = {{1, 15}, {3, 6}, {4, 9}, {6, 10}, {7, 0}};

with the meaning 15 in [1,3), 6 in [3,4),... and 10 in [6,7) You see the last one is just for saying how large the last bin is. Now you can use:

Second[x_] := x[[2]]
Histogram[Drop[Map[First, myData], -1], Map[First, myData] &, Drop[Map[Second, myData], -1] &]

With the result

$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – user9660 Feb 9 '16 at 9:55

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