0
$\begingroup$

I want to tick the Y axes so that the heights of the displayed bins in range, represent the share respect to total available points.

I have a distribution of percentages on N data points (see below for clarity).

  • I want to plot only the ones falling within range 0 - 1. (100%), in bins of 0.1; values > 1. will not be plotted;

  • I want to tick on the Y Axes the percentage representing how many points a bin represent, respect to total points


Example:

x is the data below.

Histogram[x] shows all points. I want only a range, so:

Histogram[x, {0, 1., .1}] that shows only two bins, one with 6 values, and one with 3.

Now, I want to report ticks on the Y axes to mark the height of the bars, as relative percentage to the total available points.

So the maximum Y' could be:

Total[Select[# < 1. &]@x]/Length[x] 

that is, the number of values displayed in the histogram, respect to total (I don't see values of x not in range).

In percentage, a max value of Y axes would be ~2% of total values:

Round[Total[Select[# < 1. &]@x]/Length[x] * 100]

How to tick the Y axes with percentages from 0 to 2% , indicating the height of bins as "share" respect to total points ?


I looked at https://stackoverflow.com/a/7708628/305883.

I understood that "probability" will use the fraction of values respect to the values displayed in the histogram, and won't count for values out of range.

Did I understand correctly?

Answer of @JimB shows a simpler direction, but I am confused: the sum of bins' frequency does not match with the sum the number of values in range: Total[Select[# < 1. &]@x]/Length[x]

(in the example from suggested answer: 0.0146341 + 0.00731707 != Total[Select[# < 1. &]@x]/Length[x] )

I received feedback to improve my question, so I edited it and I hope it is clearer now.

I leave my previous attempt for learning.


First attempt to mark percentage on Y axes, to indicate % of values represented by bars respect to total points.

(* here I try to compute the share of a values on Y axes respect to title size of my sampling, including values not in Range *)
bins = {Range[0, 1, 0.1]};
Histogram[x, bins, "Probability", 
 Ticks -> {First@bins, 
   Table[{0.01 i, 
     If[Mod[i, 10] == 0 ToString[i /Length[x]*100 // N] <> "%", 
      i]}, {i, 100}]}]

Second attempt

(* here I save heights repsect to histogram where I display all values, and then use my own bins between 0, 1. *)

{bins, heights} = HistogramList[x];

bins = {Range[0, 1, 0.1]};
Histogram[x, bins, "Probability", 
 Ticks -> {First@bins, 
   Table[{0.01 i, 
     If[Mod[i, 10] == 0 ToString[i /Total[heights]*100 // N] <> "%", 
      i]}, {i, 100}]}]

data

x = {2.09143, 1.57714, 2.91429, 1.2, 1.57714, 1.78286, 1.57714, 1.54286, \
1.11429, 2.05714, 2.22857, 1.54286, 1.71429, 2.70857, 2.57143, \
6.17143, 2.05714, 1.02857, 1.30286, 1.61143, 1.71429, 1.2, 2.48571, \
1.54286, 3.63429, 1.64571, 1.23429, 2.43429, 1.88571, 2.22857, \
2.05714, 1.2, 1.88571, 1.33714, 1.54286, 1.71429, 2.22857, 1.88571, \
2.57143, 2.22857, 1.30286, 2.57143, 5.00571, 6.51429, 2.4, 2.46857, \
1.54286, 1.98857, 1.88571, 2.14286, 2.4, 2.57143, 2.57143, 1.54286, \
1.37143, 1.54286, 2.05714, 2.02286, 1.50857, 2.22857, 2.22857, \
1.54286, 1.71429, 1.13143, 1.45714, 1.71429, 1.71429, 2.57143, \
1.62857, 2.26286, 2.57143, 0.857143, 2.91429, 1.30286, 1.66286, \
2.22857, 1.95429, 1.88571, 1.68, 1.88571, 2.53714, 1.2, 1.85143, \
2.74286, 1.71429, 2.22857, 2.4, 2.4, 1.54286, 1.2, 1.71429, 1.88571, \
1.54286, 1.71429, 2.05714, 1.81714, 1.57714, 1.88571, 1.45714, \
1.62857, 1.78286, 1.56, 1.54286, 1.30286, 1.88571, 2.05714, 1.64571, \
2.05714, 1.81714, 2.22857, 1.81714, 2.4, 1.45714, 2.4, 2.48571, \
1.88571, 1.71429, 1.71429, 1.2, 1.30286, 1.13143, 2.05714, 2.22857, \
1.2, 1.23429, 2.22857, 1.37143, 1.09714, 1.09714, 1.09714, 1.2, \
2.22857, 2.31429, 2.22857, 2.57143, 2.4, 2.4, 2.05714, 2.05714, \
3.42857, 1.37143, 2.57143, 1.68, 1.73143, 0.685714, 1.02857, 1.02857, \
1.68, 1.2, 1.71429, 1.71429, 1.47429, 2.22857, 1.02857, 1.37143, \
2.74286, 1.54286, 1.2, 1.2, 1.54286, 2.05714, 3.80571, 1.2, 2.74286, \
2.05714, 2.05714, 1.30286, 2.22857, 2.57143, 2.74286, 1.71429, \
1.54286, 1.2, 1.09714, 1.88571, 1.95429, 1.2, 1.37143, 1.2, 1.23429, \
1.44, 1.2, 1.2, 2.22857, 1.37143, 2.4, 1.71429, 1.71429, 1.37143, \
0.685714, 1.88571, 1.54286, 1.88571, 1.57714, 1.57714, 2.22857, \
1.40571, 1.2, 1.2, 2.91429, 1.88571, 1.88571, 0.685714, 1.2, 1.57714, \
1.57714, 2.14286, 1.71429, 0.685714, 2.14286, 1.09714, 0.685714, \
2.05714, 1.02857, 1.37143, 1.81714, 1.81714, 1.95429, 1.95429, \
1.30286, 2.4, 1.26857, 1.54286, 1.54286, 2.33143, 1.37143, 1.71429, \
1.47429, 2.22857, 1.71429, 1.71429, 1.98857, 2.05714, 1.81714, \
1.54286, 1.02857, 2.05714, 1.54286, 1.37143, 1.37143, 1.88571, \
1.37143, 2.33143, 2.05714, 1.92, 1.88571, 1.54286, 1.2, 2.22857, \
2.74286, 2.22857, 1.37143, 1.71429, 2.05714, 1.88571, 1.71429, \
1.54286, 1.2, 1.37143, 1.30286, 1.68, 1.71429, 0.857143, 1.2, \
1.37143, 1.23429, 1.23429, 1.71429, 2.09143, 1.02857, 1.88571, \
3.08571, 1.88571, 1.37143, 1.2, 2.05714, 1.28571, 2.22857, 1.28571, \
1.71429, 2.4, 1.37143, 2.05714, 1.2, 1.71429, 1.71429, 2.05714, \
1.54286, 1.37143, 1.71429, 2.4, 2.05714, 2.22857, 1.30286, 1.88571, \
1.02857, 1.71429, 1.37143, 2.05714, 2.4, 1.54286, 1.11429, 1.71429, \
1.37143, 2.05714, 1.62857, 1.71429, 1.64571, 2.4, 1.71429, 2.05714, \
1.13143, 0.891429, 1.54286, 1.54286, 2.05714, 2.22857, 1.88571, \
1.02857, 1.54286, 1.37143, 2.05714, 1.95429, 1.28571, 1.28571, \
1.54286, 1.54286, 1.71429, 1.71429, 1.64571, 2.05714, 1.47429, \
1.88571, 2.29714, 1.02857, 1.2, 1.54286, 1.02857, 1.71429, 1.54286, \
1.71429, 1.68, 1.71429, 1.54286, 0.685714, 1.47429, 1.71429, 1.88571, \
1.54286, 1.54286, 1.2, 1.13143, 1.37143, 2.31429, 2.31429, 1.88571, \
2.05714, 2.05714, 2.05714, 1.37143, 1.71429, 1.71429, 1.71429, \
1.37143, 1.02857, 1.88571, 1.88571, 2.05714, 2.74286, 1.71429, \
1.71429, 1.71429, 2.02286, 1.57714, 1.37143, 1.26857, 1.37143, \
1.88571, 1.64571, 1.37143, 2.05714, 1.54286, 1.2, 1.88571, 2.05714, \
1.54286, 1.02857, 1.02857, 1.71429, 1.61143, 1.71429, 1.71429, \
1.37143, 1.30286, 1.71429, 1.37143, 1.54286, 1.88571, 1.37143, \
1.88571, 1.68, 2.05714, 1.37143, 2.4, 1.37143, 1.37143, 1.37143, \
1.54286, 1.68, 1.18286}

Histogram[x, {0, 1, 0.1}]
$\endgroup$

closed as off-topic by JimB, José Antonio Díaz Navas, bbgodfrey, Yves Klett, Bob Hanlon Nov 20 '18 at 18:20

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – JimB, José Antonio Díaz Navas, bbgodfrey, Yves Klett, Bob Hanlon
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ The option is "Probability" and not Probability. $\endgroup$ – JimB Nov 16 '18 at 14:09
  • $\begingroup$ you're right @Jim but does not change results - still can't see share ticks on axes $\endgroup$ – user305883 Nov 16 '18 at 14:20
  • 1
    $\begingroup$ Understood. But not seeing desired ticks is not in your question. Please make your question explicit in the question rather than in the comments. I'm not trying to be picky as the reason for this forum is to provide an answer for you AND for others who might have the same question. But when the question is unclear or keeps changing, then that will not likely help anyone else. $\endgroup$ – JimB Nov 16 '18 at 16:18
  • 1
    $\begingroup$ It seems the only difference in the current answer I provided below is that the vertical axis is a "proportion" and you want "percentage". I'll make that change. However, your formula for comparing the proportion of points less than 1 is incorrect: Total[Select[# < 1. &]@x]/Length[x] should be N[Length[Select[# < 1. &]@x]/Length[x]]. Using the correct formula matches what I provided. $\endgroup$ – JimB Nov 16 '18 at 19:43
  • $\begingroup$ ouch, sure. Thank you again @JimB for taking time to check on my question! $\endgroup$ – user305883 Nov 17 '18 at 14:01
2
$\begingroup$

I'm not at all certain what you're asking but here is one interpretation.

First, here is a histogram of the complete dataset:

Histogram[x, Automatic, "Probability", PlotRange -> All, Frame -> True]

Histogram of all data

Now here is that same histogram but only showing part of the horizontal axis:

Histogram[x, Automatic, "Probability", PlotRange -> {{0, 1}, {0, 0.02}},
 Frame -> True, PlotRangePadding -> {{Automatic, -0.01}, {0, 0}}]

Histogram with restricted range

Update:

If the vertical axis needs to be a percentage rather than a proportion, then I don't see that Histogram has such an option. Maybe the use of the ScalingFunctions option might do it but I did not have success with that. The brute-force alternative is to relabel the vertical axis with your own ticks. (I also matched the binwidth to what was requested in the question.)

ticks = {{Table[
  If[OddQ[i], {i/1000., "", {0.01, 0}},
   {i/1000., NumberForm[i/10., {2, 1}], {0.015, 0}}], {i, 0, 20, 1}], 
   Automatic}, {Automatic, None}};
Histogram[x, {0.1}, "Probability", Frame -> True, 
  PlotRange -> {{0, 1}, {0, 0.02}}, FrameTicks -> ticks,
  PlotRangePadding -> {{Automatic, -0.01}, {0, 0}}]

Histogram with percentage vertical scale

$\endgroup$
  • $\begingroup$ thank your for your feedback @JimB: I updated the question, hope it is clearer. $\endgroup$ – user305883 Nov 16 '18 at 18:21

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