对数范围 [英] Logarithmic range

查看:176
本文介绍了对数范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常稀疏的数据集,我想将其以对数刻度绘制在直方图中.我希望X轴看起来类似于:

I have a very sparse dataset and I want to plot it in a histogram with a logarithmic scale. I would like the X axis to look something similar to:

[1、10、100、1000、10000]

[1, 10, 100, 1000, 10000]

意味着第一个bin将包含成本为1-10的观察值,第二个11-10以此类推,最后一个将是+10.000.

Meaning that the first bin would include the observations with a cost of 1-10, the second 11-10 and so on, and the last would be +10.000.

function (pow, min, max) {
    var range = [];         
    for (i = min; i < max; i++) {
        range.push(Math.pow(pow, i));
    }
    return range;
}

我能够生成给定的10的幂的数组.现在,我想将一组连续值映射到该数组,这意味着:

I am able to generate the given array of powers of 10. Now, what I would like is to map a set of continuous values to that array, meaning that:

[1、23、2、105、2000、30000]

[1, 23, 2, 105, 2000, 30000]

将输出为:

[1、10、1、100、1000、10000]

[1, 10, 1, 100, 1000, 10000]

我尝试使用d3.scale.threshold,但似乎无法按预期工作.

I have tried using d3.scale.threshold, but it does not seem to be working as expecting.

推荐答案

A d3阈值比例根据您选择的阈值点将定量数据集分为离散类别.

A d3 Threshold Scale splits a quantitive dataset into discrete categories based on threshold points that you choose.

不同于简单的线性标度,阈值标度的 domain 不是最小和最大值.相反,阈值标度的域是要在其上拆分数据的阈值列表.

Unlike simple linear scales, the domain of the the threshold scale is not the min and max values. Instead, the threshold scale's domain is the list of threshold values at which you want to split your data.

与所有d3刻度一样,范围是所需的输出值.但是,对于阈值刻度,该范围内的输出值的数量必须等于阈值(域)值的数量加1 .范围内的第一个值分配给小于第一阈值的值,范围内的第二个值分配给等于或大于第一个阈值但小于第二个阈值的值,依此类推,直到范围中的最后一个值分配给等于或大于域中最后一个值的值.

As with all d3 scales, the range is the desired output values. For a threshold scale, however, the number of output values in the range has to be equal to the number of threshold (domain) values plus one. The first value in the range is assigned to values that are less than the first threshold, the second value in the range is assigned to values that are equal to or greater than the first threshold but less than the second, and so on, until the last value in the range is assigned to values that are equal to or greater than the last value in the domain.

因此,要基于10的幂将正整数数组划分为类别,可以将10的幂用作 domain ,并使用一组将类别描述为的字符串范围:

So to split an array of positive integers into categories based on powers of 10, you could use the powers of 10 as your domain, and a set of strings describing the categories as your range:

scale = d3.scale.threshold()
          .domain([1, 11, 101, 1001, 10001])
          .range(["<1", "1-10", "11-100", "101-1000", "1001-10,000", ">10,001"]);

当然,根据您使用的比例尺,建议使用诸如@Daniele Torino之类的自定义函数可能更具通用性,因为它不需要您指定高于/低于/低于的最大值和最小值阈值,所有东西都聚集在其中同一类别.但是,如果您想要的是 ,则可以使用上面的阈值刻度.

Of course, depending on what you are using the scale for, a custom function like @Daniele Torino suggested may be more versatile, since it doesn't require you to specify max and min threshold above/below which everything gets bunched in the same category. But if that is what you want, the threshold scale above should do it.

这篇关于对数范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆