【问题标题】:Why point closeness is differing from scatter plot to hexbin plot?为什么点接近度不同于散点图和六边形图?
【发布时间】:2017-03-31 14:12:59
【问题描述】:

请看Hexbin和散点图:http://imgur.com/a/2oR68

为什么在 Hexbinplot 中,点不相互接触,而在散点图中却清楚地接触到接近的点?

我预计我的 hexbin 情节会像这样出现:https://bl.ocks.org/mbostock/4248145 但它没有。

我正在使用 d3.hexbin 插件。

除了一点点缩放之外,从 Hexbin 图到散点图(我正在处理相同的数据集)的唯一代码是:

对于Hexbin:

 var color = d3.scale.linear()
                .range(["white", "steelblue"])
                .interpolate(d3.interpolateLab);

var hexbin = d3.hexbin()
               .extent([[0,0],[size - padding , padding]])
               .radius();

hexbin.x(function(d,i){return x(subdata[0][i]);})
hexbin.y(function(d,i){return y(subdata[0][i]);})

svg.append("clipPath")
    .attr("id", "clip")
    .append("rect")
    .attr("class", "mesh")
    .attr("width", w)
    .attr("height", size);

svg.append("g")
    .attr("clip-path", "url(#clip)")
    .selectAll(".hexagon")
    .data(hexbin(datum))
    .enter()
    .append("path")
    .attr("class", "hexagon")
    .attr("d", hexbin.hexagon())
    .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
    .style("fill", function(d) { return color(d.length); });    

散点图:

 svg.selectAll("circle")
.data(datum)
.enter()
.append("circle")
.style("fill", "steelblue")
.attr("cx", function (d, i) {
    return x(subdata[0][i]);
})
.attr("cy", function (d,i) {
    return y(subdata[0][i]);
})
.attr("r", 3)

我哪里做错了?

Edit1:在 Hexbin 下包含部分代码

【问题讨论】:

    标签: d3.js


    【解决方案1】:

    如果你设置...

    .attr("d", hexbin.hexagon(5))
    //radius value here ------^
    

    ...,只有在六边形生成器中设置相同的值时,六边形才会接触:

    var hexbin = d3.hexbin()
        .radius(5)//same value here
        .extent([[0, 0], [width, height]]);
    

    根据您的结果,我认为情况并非如此。因此,解决方案可以简单地删除该值:

    .attr("d", hexbin.hexagon())
    //no radius here --------^
    

    【讨论】:

    • 嗨 Gerardo,即使进行了指定的更改,它仍然没有更改。我也将编辑包含半径部分的代码。我正在使用 d3 版本 3
    • 你必须在var hexabin中设置半径。
    • 是的,我做到了。我将 var hexbin 中的半径更改为 radius(10) 和 .attr("d", hexbin.hexagon(10))....而且,我尝试了不同的其他组合...可能是我在其他地方做错了代码...?
    • 可能。最好的想法是使用 fiddle/plunker/codepen/whatever 创建一个工作代码。
    • 我在这里创建了一个虚拟数据集:jsfiddle.net/data_x/5L2hf0zx/46 它向我展示了一个奇怪的 hexbin.x 错误不是我在 ide 中测试时没有得到的函数......我想我是在这里做错了......我目前正在处理这个......谢谢,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多