6
$\begingroup$

I have a bit of a strange problem. I'd like to overlay 3 different logarithmic Histograms with two of them stacked and one separate [*]. Without weights the solution is simple:

data1 = RandomVariate[NormalDistribution[1, 3], 10^3];
data2 = RandomVariate[NormalDistribution[5, 3], 10^3];
data3 = RandomVariate[NormalDistribution[10, 3], 10^3];
Show[Histogram[{data1, data2}, ChartLayout -> "Stacked", ScalingFunctions -> {None, "Log"}],Histogram[data3, ChartStyle -> Red, ScalingFunctions -> {None, "Log"}]]

However, this doesn't look nice when I have weighted data and I can't figure out a way to fix it. The issue is that Mathematica doesn't know where to end the histogram of data1 and data2. I tried the following:

data1 = WeightedData[RandomVariate[NormalDistribution[1, 3], 10^3], Table[1, {i, 1, 10^3}]];
data2 = WeightedData[RandomVariate[NormalDistribution[5, 3], 10^3], Table[1, {i, 1, 10^3}]];
data3 = WeightedData[RandomVariate[NormalDistribution[10, 3], 10^3], Table[0.1, {i, 1, 10^3}]];

Show[Histogram[{data1, data2}, ChartLayout -> "Stacked", ScalingFunctions -> {None, "Log"}], Histogram[data3, ChartStyle -> Red, ScalingFunctions -> {None, "Log"}]]

which gives,

$\hspace{3cm}$enter image description here

Swapping which histogram is first in Show makes things worse and playing with PlotRange was also no use.

[*] I emphasize that I'd like two of this histograms stacked because I think one can put all the histograms in a single Histogram function if this is not the case and there is no problem any more.

$\endgroup$
4
  • 1
    $\begingroup$ This must be a Version 10.0.0 /10.0.1 issue; your code works as expected in Version 9 (Windows 8 64bit). If it is, perhaps it should have the version tag? $\endgroup$
    – kglr
    Sep 18, 2014 at 17:31
  • $\begingroup$ @kguler: My version is indeed 10.0.0. I updated the tag. $\endgroup$
    – JeffDror
    Sep 18, 2014 at 17:40
  • $\begingroup$ The issue remains in version 10.0.1.0. $\endgroup$
    – kglr
    Sep 18, 2014 at 17:43
  • $\begingroup$ The issue remains in version 10.0.2; Can someone please check in 10.1.0, so to tag this as a bug (isn't it?). Also, does anyone know if this has been reported to WR? This problem also happens, for a single Histogram, where we enter a specific plot range. $\endgroup$
    – P. Fonseca
    May 23, 2015 at 19:37

1 Answer 1

6
$\begingroup$

Even the uneweighted data can have display (offset) problems when using Show. Also the data3 histogram obscures portions of the stacked histogram.

SeedRandom[500];

data1 = RandomVariate[NormalDistribution[1, 3], 10^3];
data2 = RandomVariate[NormalDistribution[5, 3], 10^3];
data3 = RandomVariate[NormalDistribution[10, 3], 10^3];

Show[
 Histogram[{data1, data2},
  ChartLayout -> "Stacked",
  ScalingFunctions -> {None, "Log"}],
 Histogram[data3,
  ChartStyle -> Red,
  ScalingFunctions -> {None, "Log"}],
 Frame -> True, Axes -> False]

enter image description here

Create the stacked histogram data manually and use a single Histogram

data12 = Join[data1, data2]; (* stacked data *)

Histogram[{data12, data1, data3},
 ScalingFunctions -> {None, "Log"},
 Frame -> True, Axes -> False]

enter image description here

data1w = WeightedData[
   RandomVariate[
    NormalDistribution[1, 3], 10^3],
   Table[1, {10^3}]];
data2w = WeightedData[
   RandomVariate[
    NormalDistribution[5, 3], 10^3],
   Table[1, {10^3}]];
data3w = WeightedData[
   RandomVariate[
    NormalDistribution[10, 3], 10^3],
   Table[0.1, {10^3}]];

Similarly for the weighted data

data12w = WeightedData[
   Join[data1w[[2, 1]], data2w[[2, 1]]],
   Join[data1w[[2, 2]], data2w[[2, 2]]]];
(* stacked data *)

Histogram[{data12w, data1w, data3w},
 ScalingFunctions -> {None, "Log"}]

enter image description here

$\endgroup$

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.