Message Boards Message Boards

Create stacked histograms?

Posted 8 years ago

I have to compare histogram of rational numbers, associated each time with 3 dataset and, sometimes, one of these datasets is empty.

Example: consider

Data = {{3/194, 1/194, 1/194, 1/194, 2/97, 3/194, 1/194, 2/97, 9/194, 
    1/97, 1/194, 7/194, 1/194, 1/97, 2/97, 3/194, 2/97, 1/194, 2/97, 
    1/194, 1/194, 1/194, 2/97, 1/194, 4/97, 4/97, 1/194, 3/194, 1/194,
     3/194, 2/97, 1/194, 1/97, 1/194, 4/97, 7/194, 1/194, 1/194, 2/97,
     5/194, 1/97, 1/194, 1/194, 5/194, 3/194, 1/97, 5/194, 3/194, 
    1/194, 1/194, 1/97, 3/194, 3/194, 1/97, 3/194, 1/194, 2/97, 1/97, 
    1/97, 3/194, 1/97, 1/97, 3/194, 1/194, 1/194, 2/97, 1/97, 1/97, 
    3/194, 2/97, 1/97, 5/194, 3/194, 3/194, 1/194, 3/194, 1/97, 3/194,
     1/97, 1/194, 2/97, 1/97, 1/97, 2/97, 1/97, 1/97, 1/97, 1/194, 
    1/97, 1/194, 1/194, 5/194, 5/194, 1/97, 1/194, 1/194, 1/97, 3/194,
     5/194, 1/194, 3/194, 1/194, 3/194, 1/194, 2/97, 1/194, 3/194, 
    3/194, 1/194, 1/97, 1/194, 1/194, 1/97, 3/194, 3/194, 3/97, 1/194,
     3/194, 2/97, 1/194, 5/194, 1/194, 1/97, 3/194, 2/97, 1/194, 
    3/194, 5/194, 1/97, 2/97, 1/97, 2/97, 2/97, 5/194, 1/194, 1/194, 
    1/194, 1/97, 5/194, 2/97, 3/194, 1/194, 2/97, 1/194, 3/97, 2/97, 
    1/194, 1/194, 1/97, 2/97, 3/194, 1/194, 1/97, 1/194, 1/194, 1/97, 
    3/194, 1/194, 1/194, 1/194, 2/97, 1/194, 1/194, 2/97, 1/194, 
    1/194, 2/97, 3/194, 2/97, 3/194, 2/97, 1/194, 1/97, 1/194, 1/194, 
    3/194, 1/97, 1/194, 3/194, 1/194, 3/194, 1/97, 1/194, 3/194, 
    1/194, 1/194, 1/194, 1/194, 5/194, 1/97, 2/97, 1/97, 1/194, 2/97, 
    2/97, 1/194, 1/97, 1/194, 2/97, 5/194, 1/194, 1/97, 1/194, 1/194, 
    1/97, 2/97, 1/194, 1/97, 1/194, 2/97, 3/194, 5/194, 1/194, 2/97, 
    1/194, 2/97, 1/194, 1/194, 2/97, 3/194, 1/97, 1/194, 1/97, 1/97, 
    5/194, 3/194, 3/194, 3/194, 1/194, 1/194, 1/97, 1/97, 3/194, 2/97,
     1/97, 1/97, 3/194, 1/97, 1/97, 1/97, 1/194, 2/97, 1/194, 1/97, 
    1/194, 1/97, 1/194}, {1/97, 1/97}, {1/97, 3/194, 3/194, 1/194, 
    1/97, 1/97, 3/194, 3/194, 1/194, 3/194, 1/97, 1/194, 1/194, 1/97, 
    1/194, 1/194, 1/194, 1/194, 3/194, 1/97, 1/194, 1/194, 1/97, 
    1/194, 1/194, 1/194, 1/194, 1/97, 1/97, 1/194, 1/97, 5/194, 1/194,
     1/97, 1/194, 1/97, 1/97, 1/194, 3/194, 3/194, 1/194, 1/194, 1/97,
     1/97, 3/194, 1/97, 1/97, 3/97, 1/97, 2/97, 1/97, 3/194, 1/97, 
    3/97, 3/194, 1/194, 1/194, 1/97, 3/97, 5/194, 1/97, 3/194, 1/194, 
    1/194, 1/97, 1/97, 1/97, 1/97, 1/97, 1/194, 3/194, 1/194, 1/194, 
    5/194, 1/194, 2/97, 3/194, 1/194, 1/97, 1/194, 3/194, 1/194, 
    1/194, 1/97, 3/194, 1/194, 7/194, 1/97, 3/194, 3/194, 2/97, 1/97, 
    1/97, 1/194, 2/97, 1/194, 2/97, 1/97, 3/97, 1/97, 1/97, 1/194, 
    1/97, 3/194, 5/194, 5/194, 1/194, 3/194, 1/97, 1/194, 3/97, 1/97, 
    1/194, 2/97, 5/97, 1/194, 1/194, 2/97, 3/97, 1/194, 1/97, 4/97, 
    1/194, 4/97, 1/97, 1/194, 2/97, 1/194, 1/194, 1/194, 3/194, 1/194,
     1/194, 1/97, 1/194, 1/194, 1/97, 3/194, 1/194, 1/194, 5/194, 
    3/97, 3/194, 2/97, 1/194, 1/97, 1/97, 3/194, 3/194, 3/194, 1/97, 
    1/97, 1/97, 1/194, 1/194, 1/194, 1/97, 3/194, 1/97, 2/97, 3/194, 
    1/194, 5/194, 1/97, 2/97, 1/97, 1/97, 1/97, 3/194, 2/97, 1/97, 
    1/97, 4/97, 1/97, 1/194, 2/97, 1/97, 1/97, 3/194, 3/194, 2/97, 
    1/97, 1/194, 3/194, 2/97, 1/97, 1/97, 3/194, 1/194, 5/194, 1/97, 
    3/194, 1/194, 1/194, 1/194, 2/97, 1/194, 1/97, 1/97, 3/194, 1/97, 
    1/97, 1/97, 3/194, 7/194, 1/97, 3/194, 3/194, 1/194, 1/194, 2/97, 
    1/97, 1/194, 1/97, 1/194, 2/97, 1/97, 3/194, 3/194, 11/194, 5/194,
     1/194, 3/194, 3/97, 1/194, 1/194, 2/97, 1/194, 3/194, 1/97, 
    3/194, 1/97, 1/194, 2/97, 1/194, 3/194, 1/97, 1/97, 1/194, 1/194, 
    1/194, 1/97, 3/194, 3/194, 1/194, 1/97, 1/97, 1/97, 3/194, 3/194, 
    1/97, 1/194, 3/194, 1/194, 3/194, 1/97, 1/194, 1/194, 1/97, 1/194,
     3/194, 1/194, 1/194, 1/97, 1/97, 1/194, 1/97, 3/194, 2/97, 1/97, 
    3/194, 1/194, 1/97, 2/97, 1/194, 3/194, 1/97, 2/97, 5/194, 1/194, 
    1/97, 1/97, 1/97, 1/194, 1/194, 3/194, 2/97, 1/97, 1/194, 1/97, 
    1/194, 1/194, 2/97, 2/97, 1/194, 1/194, 5/194}};
Masques = {"Matrice", "Résine", "Revetement"};
h = 1/194;

Then, build 2 lists, with an empty member:

artificial1 = {First@Data, Last@Data, {}};
artificial2 = {First@Data, {}, Last@Data};

and plot these histograms:

Histogram[Data, {h}, "Probability", 
 ChartLegends -> Placed[Masques, Above] , 
 ChartStyle -> {Red, Green, Blue, Yellow}, ChartLayout -> "Stacked", 
 ImageSize -> Medium]
Histogram[artificial1, {h}, "Probability", 
 ChartLegends -> Placed[Masques, Above] , 
 ChartStyle -> {Red, Green, Blue, Yellow}, ChartLayout -> "Stacked", 
 ImageSize -> Medium]

As it was requested, they are stacked. But the third one, with an intermediate empty list, is not stacked!

Histogram[artificial2, {h}, "Probability", 
 ChartLegends -> Placed[Masques, Above] , 
 ChartStyle -> {Red, Green, Blue, Yellow}, ChartLayout -> "Stacked", 
 ImageSize -> Medium]

Is it a bug, or did I made a mistake somewhere?

Best regards, Claude

POSTED BY: Claude Mante
Posted 8 years ago

I don't see any mistake or the rationale for how Mathematica is choosing the colors when one of the rows of data is empty. I also think that stacked histograms and stacked barcharts should be a criminal offense especially when the "Probability" option is chosen. However, here's a way to achieve a consistent color scheme automatically.

Masques = {"Matrice", "RĂ©sine", "Revetement"};
h = 1/194;
artificial1 = {Data[[1]], Data[[2]], {}};
artificial2 = {Data[[1]], {}, Data[[3]]};
colors = {Red, Green, Blue};

keep = (Length[#] > 0 &) /@ Data;
Histogram[Pick[Data, keep], {h}, "Probability", 
 ChartLegends -> Placed[Masques, Above], 
 ChartStyle -> Pick[colors, keep], ChartLayout -> "Stacked", 
 ImageSize -> Medium]

keep = (Length[#] > 0 &) /@ artificial1;
Histogram[Pick[artificial1, keep], {h}, "Probability", 
 ChartLegends -> Placed[Pick[Masques, keep], Above], 
 ChartStyle -> Pick[colors, keep], ChartLayout -> "Stacked", 
 ImageSize -> Medium]

keep = (Length[#] > 0 &) /@ artificial2; Histogram[
 Pick[artificial2, keep], {h}, "Probability", 
 ChartLegends -> Placed[Pick[Masques, keep], Above], 
 ChartStyle -> Pick[colors, keep], ChartLayout -> "Stacked", 
 ImageSize -> Medium]

Stacked histogram with all data Stacked histogram with last row empty Stacked histogram with middle row empty

Note that I've changed the definition of artificial1 so that the relative bar sizes can be compared across the figures to be more convincing.

POSTED BY: Jim Baldwin
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract