Skip to content

密度估计

¥Density estimation

轮廓图可以显示点云的估计密度,这有助于避免在大型数据集中过度绘图。contourDensity 方法实现了快速二维核密度估计。

¥Contours can show the estimated density of point clouds, which is useful to avoid overplotting in large datasets. The contourDensity method implements fast two-dimensional kernel density estimation.

以下散点图显示了老忠实间歇泉的空闲时长和喷发时长之间的关系:

¥Here is a scatterplot showing the relationship between the idle duration and eruption duration for Old Faithful:

以下是密度等值线图,显示了 53,940 颗钻石的重量和价格之间的关系:

¥And here is a density contour plot showing the relationship between the weight and price of 53,940 diamonds:

contourDensity()

示例 · 源代码 · 使用默认设置构造一个新的密度估计器。

¥Examples · Source · Constructs a new density estimator with the default settings.

density(data) {#_density}

源代码 · 估计给定数据数组的密度轮廓线,返回一个包含 GeoJSONMultiPolygon几何对象 的数组。

¥Source · Estimates the density contours for the given array of data, returning an array of GeoJSON MultiPolygon geometry objects.

每个几何对象都表示估计的每平方像素点数大于或等于相应 阈值 的区域;每个几何对象的阈值以 geometry.value 的形式公开。返回的几何对象通常传递给 geoPath 进行显示,并使用 null 或 geoIdentity 作为关联的投影。另请参阅 contours

¥Each geometry object represents the area where the estimated number of points per square pixel is greater than or equal to the corresponding threshold value; the threshold value for each geometry object is exposed as geometry.value. The returned geometry objects are typically passed to geoPath to display, using null or geoIdentity as the associated projection. See also contours.

每个数据点的 x 和 y 坐标均使用 density.xdensity.y 计算。此外,density.weight 表示每个数据点的相对贡献(默认值为 1)。生成的轮廓仅在估算器的 定义的尺寸 范围内准确。

¥The x and y coordinate for each data point are computed using density.x and density.y. In addition, density.weight indicates the relative contribution of each data point (default 1). The generated contours are only accurate within the estimator’s defined size.

density.x(x) {#density_x}

源代码 · 如果指定了 x,则设置 x 坐标访问器。如果未指定 x,则返回当前的 x 坐标访问器,默认为:

¥Source · If x is specified, sets the x-coordinate accessor. If x is not specified, returns the current x-coordinate accessor, which defaults to:

js
function x(d) {
  return d[0];
}

density.y(y) {#density_y}

源代码 · 如果指定了 y,则将 y 坐标访问器设置为指定的函数或数字,并返回此区域生成器。如果未指定 y,则返回当前的 y 坐标访问器,默认为:

¥Source · If y is specified, sets the y-coordinate accessor. If y is not specified, returns the current y-coordinate accessor, which defaults to:

js
function y(d) {
  return d[1];
}

density.weight(weight) {#density_weight}

源代码 · 如果指定了 weight,则设置点权重的访问器。如果未指定权重,则返回当前点权重访问器,默认为:

¥Source · If weight is specified, sets the accessor for point weights. If weight is not specified, returns the current point weight accessor, which defaults to:

js
function weight() {
  return 1;
}

density.size(size) {#density_size}

源代码 · 如果指定了 size,则将 size 设置为指定的函数或数字并返回此符号生成器。size 指定为数组 [width, height],其中 width 是 x 轴的最大值,height 是 y 轴的最大值。如果未指定 size,则返回当前大小,默认为 [960, 500]。估计密度轮廓线 仅在定义的尺寸范围内准确。

¥Source · If size is specified, sets the size of the density estimator to the specified bounds and returns the estimator. The size is specified as an array [width, height], where width is the maximum x-value and height is the maximum y-value. If size is not specified, returns the current size which defaults to [960, 500]. The estimated density contours are only accurate within the defined size.

density.cellSize(cellSize) {#density_cellSize}

源代码 · 如果指定了 cellSize,则将底层 bin 网格中各个单元格的大小设置为指定的正整数,并返回估算器。如果未指定 cellSize,则返回当前单元格大小,默认为 4。单元大小向下舍入到最接近的 2 的幂。较小的单元可以生成更详细的轮廓多边形,但计算成本更高。

¥Source · If cellSize is specified, sets the size of individual cells in the underlying bin grid to the specified positive integer and returns the estimator. If cellSize is not specified, returns the current cell size, which defaults to 4. The cell size is rounded down to the nearest power of two. Smaller cells produce more detailed contour polygons, but are more expensive to compute.

density.thresholds(thresholds) {#density_thresholds}

源代码 · 如果指定了 thresholds,则将阈值生成器设置为指定的函数或数组并返回此轮廓生成器。如果未指定阈值,则返回当前阈值生成器,默认情况下该生成器会生成大约 20 个均匀分布的密度阈值。

¥Source · If thresholds is specified, sets the threshold generator to the specified function or array and returns this contour generator. If thresholds is not specified, returns the current threshold generator, which by default generates about twenty nicely-rounded density thresholds.

阈值定义为一个值数组 [x0, x1, …]。第一个 生成的密度轮廓线 对应于估计密度大于或等于 x0 的区域;第二条轮廓线对应的区域是估计密度大于或等于 x1 的区域,依此类推。因此,对于每个指定的阈值,只会生成一个 MultiPolygon 几何对象;阈值以 geometry.value 的形式公开。第一个值 x0 通常应大于零。

¥Thresholds are defined as an array of values [x0, x1, …]. The first generated density contour corresponds to the area where the estimated density is greater than or equal to x0; the second contour corresponds to the area where the estimated density is greater than or equal to x1, and so on. Thus, there is exactly one generated MultiPolygon geometry object for each specified threshold value; the threshold value is exposed as geometry.value. The first value x0 should typically be greater than zero.

如果指定了计数而不是阈值数组,则将生成大约 count 个均匀分布且四舍五入的阈值;参见 ticks

¥If a count is specified instead of an array of thresholds, then approximately count uniformly-spaced nicely-rounded thresholds will be generated; see ticks.

density.bandwidth(bandwidth) {#density_bandwidth}

源代码 · 如果指定了带宽,则设置高斯核的带宽(标准差)并返回估计值。如果未指定带宽,则返回当前带宽,默认为 20.4939…。指定的带宽目前会被四舍五入到此实现支持的最接近的值,并且必须为非负值。

¥Source · If bandwidth is specified, sets the bandwidth (the standard deviation) of the Gaussian kernel and returns the estimate. If bandwidth is not specified, returns the current bandwidth, which defaults to 20.4939…. The specified bandwidth is currently rounded to the nearest supported value by this implementation, and must be nonnegative.

density.contours(data) {#density_contours}

示例 · 源代码 · 返回一个轮廓(值)函数,该函数可用于计算给定数据的任意轮廓,而无需重新计算底层网格。返回的轮廓函数还公开了一个 contour.max 值,该值表示网格的最大密度。

¥Examples · Source · Return a contour(value) function that can be used to compute an arbitrary contour on the given data without needing to recompute the underlying grid. The returned contour function also exposes a contour.max value which represents the maximum density of the grid.