Skip to content

分位数尺度

¥Quantile scales

分位数比例将采样的输入域映射到离散范围。该域被视为连续的,因此比例尺可以接受任何合理的输入值;但是,域被指定为一组离散的样本值。输出范围中的值数量(基数)决定了将从域中计算出的分位数数量。为了计算分位数,需要对域进行排序,并将其视为 离散值群体;参见 quantile。请参阅 此分位数等值线图,了解示例。

¥Quantile scales map a sampled input domain to a discrete range. The domain is considered continuous and thus the scale will accept any reasonable input value; however, the domain is specified as a discrete set of sample values. The number of values in (the cardinality of) the output range determines the number of quantiles that will be computed from the domain. To compute the quantiles, the domain is sorted, and treated as a population of discrete values; see quantile. See this quantile choropleth for an example.

scaleQuantile(domain, range)

示例 · 源代码 · 使用指定的 domainrange 构造一个新的分位数比例尺。

¥Examples · Source · Constructs a new quantile scale with the specified domain and range.

js
const color = d3.scaleQuantile(penguins.map((d) => d.body_mass_g), d3.schemeBlues[5]);

如果未指定域或范围,则每个值默认为空数组。在指定域和范围之前,分位数比例无效。

¥If either domain or range is not specified, each defaults to the empty array. The quantile scale is invalid until both a domain and range are specified.

quantile(value) {#_quantile}

示例 · 源代码 · 给定输入 domain 中的值,返回输出 range 中的对应值。

¥Examples · Source · Given a value in the input domain, returns the corresponding value in the output range.

js
color(3000); // "#eff3ff"
color(4000); // "#6baed6"
color(5000); // "#08519c"

quantile.invertExtent(value) {#quantile_invertExtent}

示例 · 源代码 · 返回 domain [x0, x1] 中值与 range 中对应值的范围:quantile 的逆。

¥Examples · Source · Returns the extent of values in the domain [x0, x1] for the corresponding value in the range: the inverse of quantile.

js
color.invertExtent("#eff3ff"); // [2700, 3475]
color.invertExtent("#6baed6"); // [3800, 4300]
color.invertExtent("#08519c"); // [4950, 6300]

此方法对于交互很有用,例如,确定域中与鼠标下方像素位置对应的值。

¥This method is useful for interaction, say to determine the value in the domain that corresponds to the pixel location under the mouse.

quantile.domain(domain) {#quantile_domain}

示例 · 源代码 · 如果指定了域,则将分位数比例尺的域设置为指定的离散数值集合并返回此比例尺。

¥Examples · Source · If domain is specified, sets the domain of the quantile scale to the specified set of discrete numeric values and returns this scale.

js
const color = d3.scaleQuantile(d3.schemeBlues[5]);
color.domain(penguins.map((d) => d.body_mass_g));

数组不能为空,并且必须至少包含一个数值;NaN、null 和 undefined 值将被忽略,并且不被视为样本总体的一部分。如果给定数组中的元素不是数字,则它们将被强制转换为数字。输入数组的副本已排序并在内部存储。

¥The array must not be empty, and must contain at least one numeric value; NaN, null and undefined values are ignored and not considered part of the sample population. If the elements in the given array are not numbers, they will be coerced to numbers. A copy of the input array is sorted and stored internally.

如果未指定域,则返回比例尺的当前域(观察值集合)。

¥If domain is not specified, returns the scale’s current domain (the set of observed values).

js
color.domain() // [2700, 2850, 2850, 2900, 2900, 2900, 2900, …]

quantile.range(range) {#quantile_range}

示例 · 源代码 · 如果指定了 range 参数,则设置该范围内的离散值。

¥Examples · Source · If range is specified, sets the discrete values in the range.

js
const color = d3.scaleQuantile();
color.range(d3.schemeBlues[5]);

数组不能为空,并且可以包含任何类型的值。范围数组中的值数量(基数或长度)决定了要计算的分位数数量。例如,要计算四分位数,范围必须是包含四个元素的数组,例如 [0, 1, 2, 3]。

¥The array must not be empty, and may contain any type of value. The number of values in (the cardinality, or length, of) the range array determines the number of quantiles that are computed. For example, to compute quartiles, range must be an array of four elements such as [0, 1, 2, 3].

如果未指定范围,则返回当前范围。

¥If range is not specified, returns the current range.

js
color.range() // ["#eff3ff", "#bdd7e7", "#6baed6", "#3182bd", "#08519c"]

quantile.quantiles() {#quantile_quantiles}

示例 · 源代码 · 返回分位数阈值。

¥Examples · Source · Returns the quantile thresholds.

js
color.quantiles() // [3475, 3800, 4300, 4950]

如果 range 包含 n 个离散值,则返回的数组将包含 n 个离散值。 - 1 个阈值。小于第一个阈值的值被视为第一个分位数;大于或等于第一个阈值但小于第二个阈值的值位于第二个分位数,依此类推。在内部,阈值数组与 bisect 一起使用,查找与给定输入值关联的输出分位数。

¥If the range contains n discrete values, the returned array will contain n - 1 thresholds. Values less than the first threshold are considered in the first quantile; values greater than or equal to the first threshold but less than the second threshold are in the second quantile, and so on. Internally, the thresholds array is used with bisect to find the output quantile associated with the given input value.

quantile.copy() {#quantile_copy}

示例 · 源代码 · 返回此比例尺的精确副本。

¥Examples · Source · Returns an exact copy of this scale.

js
const c1 = d3.scaleQuantile(d3.schemeBlues[5]);
const c2 = c1.copy();

更改此比例不会影响返回的比例,反之亦然。

¥Changes to this scale will not affect the returned scale, and vice versa.