Gadfly直方图似乎选择了错误的支持

时间:2014-09-21 21:55:20

标签: histogram julia gadfly

我是Julia的新手,我正在探索可视化分布的方式。最终,我可能会回到更强大的matplotlib代码库,但我真的很喜欢IJuliaNotebook中提供的动态可视化元素。

我的问题涉及用Gadfly绘制比例值的直方图。我能够用Gadfly自动选择一个合理的支持来绘制核密度(也就是说,与基础数据一致:[ - 0.10.5])。

#Visualization
using Gadfly

#(Python) pandas analogue
require("DataFrames")

#Practice sets
require("RDatasets")

#Extract the Hedonic set
hedonic=RDatasets.dataset("plm","Hedonic")

#Define density plot layer for black population proportion
dens_layer=layer(hedonic,x=:Blacks,Geom.density,Theme(default_color=color("#de2d26")))

#Plot kernel density
dens_plot=plot(dens_layer, Guide.title("Distribution of Black Proportion"))

enter image description here

然而,直方图适用于太大的支撑([0,4])。所有相关数据都由跨越整个[0,1]间隔的单个条捕获。

#Define histogram layer
hist_layer=layer(hedonic,x=:Blacks,Geom.histogram,Theme(default_color=color("#de2d26"))) 

#Plot histogram
hist_plot_default=plot(hist_layer, Guide.title("Distribution of Black Proportion"))

enter image description here

当我增加bincount时,支持就会增长。例如,对于bincount=100,支持增长到[0,150],所有数据仍由单个条形表示。

#Plot histogram again, this time with 100 bins
hist_plot_bin100=plot(hedonic,x=:Blacks,Geom.histogram(bincount=100),Theme(default_color=color("#de2d26"))) 

enter image description here

所以,如果有人能告诉我我搞砸了什么,那肯定会受到赞赏。或者,也许限制范围会迫使直方图条的适当分配......?为此,我如何限制范围,以便我可以在[0,1]间隔上查看分布?

2 个答案:

答案 0 :(得分:1)

此问题已修复,请参阅以下结果为您的示例

julia> using Gadfly, DataFrames, RDatasets
julia> hedonic=RDatasets.dataset("plm","Hedonic")
julia> hist_layer=layer(hedonic,x=:Blacks,Geom.histogram,Theme(default_color=color("#de2d26")))
julia> hist_plot_default=plot(hist_layer, Guide.title("Distribution of Black Proportion"))

enter image description here

julia> hist_plot_bin100=plot(hedonic,x=:Blacks,Geom.histogram(bincount=100),Theme(default_color=color("#de2d26")))

enter image description here

答案 1 :(得分:0)

首先,我可以重新创造这个。我认为它在代码中的这个分支

https://github.com/dcjones/Gadfly.jl/blob/040606f82c4e014611464068b0d5cda111b6662a/src/statistics.jl#L136-L143

    isdiscrete = false
    value_set = collect(Set(values[Bool[Gadfly.isconcrete(v) for v in values]]))
    sort!(value_set)


    if  length(value_set) / length(values) < 0.9
        d, bincounts, x_max = choose_bin_count_1d_discrete(
                    values, value_set, stat.minbincount, stat.maxbincount)

这是奇怪的,因为它不是离散数据,不应该是离散箱。如果它使用choose_bin_count_1d而不是它得到更明智的答案。我认为bincount改变支持的事情可能是一个相关的错误,但不确定是怎么回事。您应该在Gadfly github页面上提交一个问题。