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()

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

密度(数据)

🌐 density(data)

来源 · 估算给定 数据 数组的密度等高线,返回一个包含 GeoJSON MultiPolygon 几何对象 的数组。

每个几何对象表示每平方像素的估计点数大于或等于相应的阈值的区域;每个几何对象的阈值作为 geometry.value暴露。返回的几何对象通常传递给geoPath进行显示,使用null或geoIdentity作为关联的投影。另见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)

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

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

密度.y(y)

🌐 density.y(y)

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

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

密度.重量(重量)

🌐 density.weight(weight)

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

js
function weight() {
  return 1;
}

density.size(size)

[来源](https://github.com/d3/d3-contour/blob/main/src/density.js) ·如果指定了大小,则将密度估计器的大小设为指定界限并返回估计量。大小被指定为数组[ width,height],其中 宽度 是最大x值, 高度 是最大y值。如果未指定大小,则返回当前大小,默认为[960, 500]。[估计密度轮廓](#_density)仅在定义尺寸内准确。

density.cellSize(cellSize)

来源 · 如果指定了 cellSize,则将底层网格中单个单元格的大小设置为指定的正整数,并返回估计器。如果未指定 cellSize,则返回当前单元格大小,默认值为 4。单元格大小会向下取整到最近的二的幂。较小的单元格会生成更详细的等高线多边形,但计算成本更高。

密度.阈值(阈值)

🌐 density.thresholds(thresholds)

来源 · 如果指定了 thresholds,则将阈值生成器设置为指定的函数或数组,并返回此等高线生成器。如果未指定 thresholds,则返回当前的阈值生成器,默认情况下会生成大约二十个圆滑的密度阈值。

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

如果指定了count而不是thresholds数组,那么将生成大约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)

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

density.contours(data)

示例 · 来源 · 返回一个 contour(value) 函数,该函数可用于在给定数据上计算任意等高线,而无需重新计算底层网格。返回的 contour 函数还提供一个 contour.max 值,表示网格的最大密度。