1
$\begingroup$

Consider, for example

Histogram[RandomVariate[NormalDistribution[0, 0.6], 1000]]

which gives me,

Now, is there a way to change the bin counts, as in, I want to multiply some factor to the bin counts (lets say "0.5"). meaning there should be only about 70 events(y-axis) at -0.5 (x-axis) instead of about 140 events. how do I reflect this on this histogram?

--Thanks much!

$\endgroup$

2 Answers 2

3
$\begingroup$

The function fh in Histogram[data, bspec, fh] is applied to two arguments: a list of bins {{b1, b2}, {b2, b3}, ...}, and a corresponding list of counts {c1, c2, ...}. The function should return a list of heights to be used for each of the ci].

So you just need to use a height function to scale the list of counts:

Histogram[data, Automatic, .5 #2 &]

enter image description here

$\endgroup$
1
  • $\begingroup$ Perfect! just what I was looking for! Thank you so so much! $\endgroup$
    – nick
    Jul 8, 2018 at 20:14
1
$\begingroup$

An alternative approach is to use the option ChartElementFunction with a function that modifies the built-in functions to change the appropriate element of rectangle coordinates:

SeedRandom[1]
data = RandomVariate[NormalDistribution[0, 0.6], 1000]; 
Histogram[data, {-2, 2, .2}, 
 ChartElementFunction -> (ChartElementData["Rectangle"][{{1, 1}, {1, .5}} #, ##2] &)] 

enter image description here

The advantages of this approach are (1) you can use other built-in height functions such as "PDF" without additional work, (2) you can have multiple datasets each with its own scaling (as long as you don't need them "Stacked"), and (3) you can use the tooltips to see the unscaled values of bar heights.

The simple method above works only for the default setting ("Bottom") for the option BarOrigin. The following function makes this approach work for non-default BarOrigin settings:

ClearAll[ceF]
ceF[sc_ : 1][cedf_ : "Rectangle", o : OptionsPattern[]] := 
    Module[{origin = Charting`ChartStyleInformation["BarOrigin"], box = #}, 
     Switch[origin, 
       Bottom, box[[2, 2]] = sc box[[2, 2]],
       Top, box[[2, 1]] = sc box[[2, 1]], 
       Left, box[[1, 2]] = sc box[[1, 2]],
       Right, box[[1, 1]] = sc box[[1, 1]]];
     ChartElementDataFunction[cedf, o][box, ##2]] &;

Examples:

 Histogram[data, {-2, 2, .2}, "PDF", ImageSize -> 300, 
  BarOrigin -> Bottom, ChartElementFunction -> ceF[.5][] ] 

enter image description here

Histogram[data, {-2, 2, .2}, ColorFunction -> "Rainbow", 
   ImageSize -> 200, BarOrigin -> #, 
   ChartElementFunction -> ceF[.5][]] & /@ {Bottom, Top, Left,  Right} // 
 Grid[Partition[#, 2], Dividers -> All] &

enter image description here

Multiple datasets each with its own scaling:

Histogram[{data, data}, {-2, 2, .2}, ChartElementFunction -> {ceF[.5][], ceF[.8][]}] 

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.