8
$\begingroup$

I would like to make a histogram with multiple datasets. Then Mathematica allows to either overlap or stack the data from the different data sets. However, I would like to have the bars side by side, but could not find a way how to do this. To illustrate what I mean:

Mathematica graphics

I want the thing on the right, but Mathematica does the thing on the left.

Thanks!

Obviously I should have supplied the data. Thanks for the answers so far - I couldn't figure out how to apply these to my data. Here they are:

{{1.25268, 0.944003, 0.508123, 0.835465, 1.08849, 0.730729, 0.539924, 
  0.458693, 0.62392, 1.14587, 0.69167, 0.969927, 0.627031, 0.371587, 
  0.477359, 0.844106, 0.414103, 1.03941, 0.95057, 0.448324, 0.419288, 
  0.362599, 0.482544, 0.129278, 0.39682, 0.838922, 0.870031, 0.566886,
   0.522641, 0.472174, 0.447978, 0.558935, 1.19184, 0.807466, 
  0.621846, 0.765641, 0.701002, 0.998617, 0.536467, 0.602834, 1.04632,
   0.563083, 0.629105, 0.493605, 0.421362, 0.716212, 0.520913, 
  0.653301, 0.688559, 0.612859, 0.21673, 0.683028, 0.381265, 0.638092,
   0.671275, 0.710335, 0.301763, 0.860698, 0.621846, 0.460767, 
  0.716212, 0.719323, 0.642931, 0.665745, 0.628759, 0.383339, 1.0999, 
  0.713446, 0.564466, 0.65261, 0.494988}, {0.698237, 0.699274, 
  0.649499, 0.673349, 0.626339, 0.576218, 0.692707, 0.626339, 0.65261,
   0.63982, 0.544418, 0.535085, 0.67888, 0.660906, 0.675769, 0.676115,
   0.665399, 0.573453, 0.450743, 0.650881, 0.643623, 0.647425, 
  0.64777, 0.645005, 0.643968, 0.69962, 0.62945, 0.55375, 0.670584, 
  0.671621, 0.659523, 0.642586, 0.330107, 0.362254, 0.63982, 0.699274,
   0.680954, 0.668856, 0.638438, 0.703768, 0.690633, 0.610785, 
  0.681991, 0.557898, 0.666091, 0.674732, 0.646388, 0.633253, 0.58486,
   0.690978, 0.605254, 0.716212, 0.659523, 0.619772, 0.672312, 
  0.688213, 0.640857, 0.679226, 0.587625, 0.591773, 0.596613, 
  0.640857, 0.624611, 0.658832, 0.640166, 0.670584, 0.64466, 0.66056, 
  0.646388, 0.528517}, {0.237815, 0.238161, 0.251988, 0.211891, 
  0.222606, 0.197373, 0.220878, 0.193571, 0.248185, 0.226409, 
  0.206015, 0.225026, 0.234013, 0.26132, 0.222952, 0.252333, 0.187003,
   0.243692, 0.211545, 0.273764, 0.193225, 0.236433, 0.238161, 
  0.230211, 0.258209, 0.246803, 0.203941, 0.254062, 0.272382, 
  0.244037, 0.231594, 0.155894, 0.23505, 0.24542, 0.208434, 0.1673, 
  0.230557, 0.248877, 0.176979, 0.218804, 0.213619, 0.272382, 
  0.253025, 0.203249, 0.227791, 0.259246, 0.212582, 0.237124, 
  0.197373, 0.221915, 0.258209, 0.277221, 0.286899, 0.17214, 0.216384,
   0.207397, 0.241618, 0.252333, 0.208434, 0.206706, 0.238852, 
  0.256827, 0.200138, 0.237124, 0.244037, 0.239889, 0.215347, 
  0.215002, 0.119945, 0.196336, 0.196336, 0.208434, 0.247148}}

Which with Histogram gives:

enter image description here

$\endgroup$
0

4 Answers 4

8
$\begingroup$

Edit:

Assuming the data you provided is called data I use

Last /@ (HistogramList[#, {0, 1.2, 0.1}] & /@ data) // 
  Transpose // BarChart

to get the following:

enter image description here

You might also want to look into the bspec part of the HistogramList documentation.

You can also use the HistogramList result for labeling/ticks.

BarChart[
   Last /@ # // Transpose, 
   ChartLabels -> {#[[1, 1]], None}]
  &@(HistogramList[#, {0, 1.2, 0.1}] & /@ data)

enter image description here

$\endgroup$
3
  • $\begingroup$ OK, that looks interesting, but the x-axis is somewhat weird. I try to get this right. $\endgroup$ Dec 6, 2012 at 14:48
  • $\begingroup$ Yeah, I know. You could probably play around with the ChartLabels placement, but the BarChart layout does not seem particularly suitable for creating nice looking histograms. $\endgroup$
    – einbandi
    Dec 6, 2012 at 14:57
  • $\begingroup$ Great thanks. I have to understand this a little more, but it works. That's most important. $\endgroup$ Dec 6, 2012 at 14:57
7
$\begingroup$

You could use BarChart together with Transpose.

datasets = {{1, 2, 3, 4, 5}, {5, 4, 3, 2, 1}, {1, 2, 1, 2, 1}};

BarChart[Transpose[datasets]]

Mathematica graphics

Edit:

Using the provided data:

datasets = {{1.25268, 0.944003, 0.508123, 0.835465, 1.08849, 0.730729,
0.539924, 0.458693, 0.62392, 1.14587, 0.69167, 0.969927, 0.627031,
0.371587, 0.477359, 0.844106, 0.414103, 1.03941, 0.95057, 
0.448324, 0.419288, 0.362599, 0.482544, 0.129278, 0.39682, 
0.838922, 0.870031, 0.566886, 0.522641, 0.472174, 0.447978, 
0.558935, 1.19184, 0.807466, 0.621846, 0.765641, 0.701002, 
0.998617, 0.536467, 0.602834, 1.04632, 0.563083, 0.629105, 
0.493605, 0.421362, 0.716212, 0.520913, 0.653301, 0.688559, 
0.612859, 0.21673, 0.683028, 0.381265, 0.638092, 0.671275, 
0.710335, 0.301763, 0.860698, 0.621846, 0.460767, 0.716212, 
0.719323, 0.642931, 0.665745, 0.628759, 0.383339, 1.0999, 0.713446,
 0.564466, 0.65261, 0.494988}, {0.698237, 0.699274, 0.649499, 
0.673349, 0.626339, 0.576218, 0.692707, 0.626339, 0.65261, 0.63982,
0.544418, 0.535085, 0.67888, 0.660906, 0.675769, 0.676115, 
0.665399, 0.573453, 0.450743, 0.650881, 0.643623, 0.647425, 
0.64777, 0.645005, 0.643968, 0.69962, 0.62945, 0.55375, 0.670584, 
0.671621, 0.659523, 0.642586, 0.330107, 0.362254, 0.63982, 
0.699274, 0.680954, 0.668856, 0.638438, 0.703768, 0.690633, 
0.610785, 0.681991, 0.557898, 0.666091, 0.674732, 0.646388, 
0.633253, 0.58486, 0.690978, 0.605254, 0.716212, 0.659523, 
0.619772, 0.672312, 0.688213, 0.640857, 0.679226, 0.587625, 
0.591773, 0.596613, 0.640857, 0.624611, 0.658832, 0.640166, 
0.670584, 0.64466, 0.66056, 0.646388, 0.528517}, {0.237815, 
0.238161, 0.251988, 0.211891, 0.222606, 0.197373, 0.220878, 
0.193571, 0.248185, 0.226409, 0.206015, 0.225026, 0.234013, 
0.26132, 0.222952, 0.252333, 0.187003, 0.243692, 0.211545, 
0.273764, 0.193225, 0.236433, 0.238161, 0.230211, 0.258209, 
0.246803, 0.203941, 0.254062, 0.272382, 0.244037, 0.231594, 
0.155894, 0.23505, 0.24542, 0.208434, 0.1673, 0.230557, 0.248877, 
0.176979, 0.218804, 0.213619, 0.272382, 0.253025, 0.203249, 
0.227791, 0.259246, 0.212582, 0.237124, 0.197373, 0.221915, 
0.258209, 0.277221, 0.286899, 0.17214, 0.216384, 0.207397, 
0.241618, 0.252333, 0.208434, 0.206706, 0.238852, 0.256827, 
0.200138, 0.237124, 0.244037, 0.239889, 0.215347, 0.215002, 
 0.119945, 0.196336, 0.196336, 0.208434, 0.247148}};

Chosing some bins:

bin = HistogramList[Flatten[datasets]][[1]]

{1/10,1/5,3/10,2/5,1/2,3/5,7/10,4/5,9/10,1,11/10,6/5,13/10}

Now using BinCounts and BarChart together:

BarChart[Transpose@(BinCounts[#, {bin}] & /@ datasets), 
 ChartLabels -> {bin, None}]

enter image description here

$\endgroup$
2
  • $\begingroup$ Cheers - that's even simpler! - Now this only messes up with the Frame-Options, which I can't assign any more. But I'll figure that out. $\endgroup$ Dec 6, 2012 at 15:13
  • $\begingroup$ @MockupDungeon There are always more ways to come up with some hand-woven solution :) $\endgroup$ Dec 6, 2012 at 15:16
2
$\begingroup$

This is no problem, just group the data you want beside each other in a list

BarChart[{{1, 2}, {3, 2}, {5, 1}}, ChartLabels -> {"A", "B"}]

which will give the nice image:

enter image description here

But if instead you want like your diagram just the labels of the groups you can do the following

BarChart[{{1, 2}, {3, 2}, {5, 1}}, ChartLabels -> {{"A", "B"}, None}]

which gives

enter image description here

$\endgroup$
2
$\begingroup$

You might want to allow for the fact that the data range in your lists may not be the same, so that binning might not be consistent. You can accommodate this by extracting the range from the data explicitly.

Create some data:

d1 = RandomInteger[{1, 5}, {100}];
d2 = RandomInteger[{2, 6}, {100}];

Form into a list:

data={d1,d2};

Extract ranges and plot data in bins of size 1.

BinCounts [#, {Min@data, Max@data, 1}] & /@ data // Transpose // BarChart

Mathematica graphics

You can see this accounts for the fact that d2 has no elements of value 1.

$\endgroup$
2
  • $\begingroup$ Thanks - that adds quite a simplification to it. $\endgroup$ Dec 6, 2012 at 14:58
  • $\begingroup$ @MockupDungeon You are welcome :) $\endgroup$ Dec 6, 2012 at 16:34

Your Answer

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

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