2
$\begingroup$

Let's consider the following test case

Clear["Global`*"];

Nt = 1000;
data = RandomReal[{0, 1}, Nt];

P0 = Histogram[data, {0, 1, 0.001}, "Probability", ChartStyle -> Gray,
ChartBaseStyle -> EdgeForm[None], Frame -> True, 
FrameLabel -> {"x", "P"}, RotateLabel -> False, 
LabelStyle -> Directive[FontFamily -> "Helvetica", 20], 
PlotRange -> {0, 0.006}, PlotRangePadding -> 0, 
PlotRangeClipping -> True, AspectRatio -> 1, ImageSize -> 550]

enter image description here

My question is: how can I multiply the values of the vertical axis by 1000?

EDIT

A part of the actual data can be found here

Then the code is

P0 = Histogram[d0, {0, 2 \[Pi], 0.001}, "Probability", 
ChartStyle -> Gray, ChartBaseStyle -> EdgeForm[None], Frame -> True,
FrameLabel -> {"\[Theta]", "P"}, RotateLabel -> False, 
LabelStyle -> Directive[FontFamily -> "Helvetica", 20], 
PlotRange -> {0, 0.0012}, PlotRangePadding -> 0, 
PlotRangeClipping -> True, AspectRatio -> 1, ImageSize -> 550]
$\endgroup$
2
  • $\begingroup$ I'm curious: why would you want such a display? It seems not to summarize the data very well. In fact, it looks like a way to compact table a table of numbers and observed frequencies but again not so much of a summary. $\endgroup$
    – JimB
    Jun 9, 2019 at 18:08
  • $\begingroup$ Doesn't using "Count" instead of "Probability" do what you want? I'm assuming that the multiplier of 1,000 is because Nt = 1000. $\endgroup$
    – JimB
    Jun 9, 2019 at 18:09

4 Answers 4

2
$\begingroup$

You can use a custom height function (you also need to change the plot range accordingly). The height function pure function takes a list of counts as the second argument, so to compute 1000 times the probability you just use:

1000 #2/Total[#2]&

Changing the plot range to 1000 times the previous plot range yields:

Histogram[
    data,
    {0,1,0.001},
    1000 #2/Total[#2]&,
    PlotRange->{0,6},
    ChartStyle->Gray,ChartBaseStyle->EdgeForm[None],Frame->True,FrameLabel->{"x","P"},
    RotateLabel->False,LabelStyle->Directive[FontFamily->"Helvetica",20],
    PlotRangePadding->0,PlotRangeClipping->True,AspectRatio->1,ImageSize->550
]

enter image description here

$\endgroup$
9
  • $\begingroup$ When I apply this method to my actual data I get the following error message: Power::infy: Infinite expression 1/0 encountered. $\endgroup$
    – Vaggelis_Z
    Jun 9, 2019 at 14:56
  • $\begingroup$ @Vaggelis_Z What gets echoed when you use 1000 Echo[#2]/Total[#2]& instead? $\endgroup$
    – Carl Woll
    Jun 9, 2019 at 14:58
  • $\begingroup$ A lot of zeroes. $\endgroup$
    – Vaggelis_Z
    Jun 9, 2019 at 15:00
  • $\begingroup$ @Vaggelis_Z What does the Histogram look like when you use "Probability" instead? $\endgroup$
    – Carl Woll
    Jun 9, 2019 at 15:02
  • $\begingroup$ Please see my edit. $\endgroup$
    – Vaggelis_Z
    Jun 9, 2019 at 15:12
1
$\begingroup$

If you look at P0 // InputForm you will see that the graphics are a series of rectangles.

For this example (with PlotRange->All) you can replace the rectangles with the following rule.

P0 /. Rectangle[{x1_Real, 0}, {x2_Real, y_Real}, "RoundingRadius" -> 0] -> 
      Rectangle[{x1,      0}, {x2,      1000*y}, "RoundingRadius" -> 0]

which results in

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ And what if the original histogram has a specific range, such as PlotRange -> {0, 0.001}? $\endgroup$
    – Vaggelis_Z
    Jun 9, 2019 at 14:08
1
$\begingroup$

Since the desired transformation is linear we can simply rescale the tick labels (using Charting`FindTicks) for the vertical axis without having to worry about modifying the third argument of Histogram:

 Row[{Show[P0, ImagePadding -> Scaled[.15]], 
  Show[P0, 
   FrameTicks -> {{Charting`FindTicks[{0, 1}, {0, 1000}], Automatic}, 
     {Automatic, Automatic}}, 
   ImagePadding -> Scaled[.15]]}]

enter image description here

Note: The option ImagePadding->Scaled[.15] is added so that the image sizes of the two objects in Row are the same (Without it, because of different label sizes, the sizes of the plot areas will be different).

$\endgroup$
0
$\begingroup$

If the multiplication by 1,000 is because Nt = 1000, then using Automatic or "Count" will get you what you want. (If you want an arbitrary multiplicative constant other than the value of Nt - and are not interested in using "Density", then it might help if you might give a rationale for doing so.)

Nt = 1000;
data = RandomReal[{0, 1}, Nt];

Histogram[data, {0, 1, 0.001}, Automatic, ChartStyle -> Gray, 
  ChartBaseStyle -> EdgeForm[None], Frame -> True, 
  FrameLabel -> {"x", "P"}, RotateLabel -> False, 
  LabelStyle -> Directive[FontFamily -> "Helvetica", 20], 
  PlotRange -> {0, 6}, PlotRangePadding -> 0, 
  PlotRangeClipping -> True, AspectRatio -> 1, ImageSize -> 550]

Histogram with counts

I don't see that this is a particular useful display of the data. You might want to try SmoothKernelDistribution instead (but using the "Bounded" option if your real data is bounded on the left and/or right).

$\endgroup$

Your Answer

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

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