How does d3.scale.quantile work?

前端 未结 2 1635
名媛妹妹
名媛妹妹 2021-02-04 05:24

What is the meaning of this statement?

quantize = d3.scale.quantile().domain([0, 15]).range(d3.range(9));

I saw that the domain is:

2条回答
  •  我寻月下人不归
    2021-02-04 05:43

    The motivation of the quantile scale is to obtain classes which are representative of the actual distribution of the values in the dataset. Therefore, it is necessary to provide it during construction with the full list of values. The scale then splits the input domain (defined by these values) into intervals (quantiles), so that about the same number of values falls into each of the intervals.

    From the documentation:

    To compute the quantiles, the input domain is sorted, and treated as a population of discrete values.

    Hence, when specifying the domain we hand in the scale the whole list of values:

    var scale = d3.scale.quantile()
      .domain([1, 1, 2, 3, 2, 3, 16])
      .range(['blue', 'white', 'red']);
    

    If we then run:

    scale.quantiles()
    

    It will output [2, 3] which means that our population of values was split into these three subsets represented by 'blue', 'white', and 'red' respectively:

    [1, 1] [2, 2] [3, 3, 16]
    

    Note that this scale should be avoided when there are outliers in the data which you want to show. In the above example 16 is an outlier falling into the upper quantile. It is assigned the same class as 3, which is probably not the desired behavior:

    scale(3)   // will output "red"
    scale(16)  // will output "red"
    

提交回复
热议问题