base histograms on StatsBase
This commit is contained in:
parent
44f4f22fdd
commit
f99957770a
1
REQUIRE
1
REQUIRE
@ -7,3 +7,4 @@ Reexport
|
|||||||
FixedSizeArrays
|
FixedSizeArrays
|
||||||
Measures
|
Measures
|
||||||
Showoff
|
Showoff
|
||||||
|
StatsBase
|
||||||
|
|||||||
@ -9,6 +9,7 @@ using Base.Meta
|
|||||||
@reexport using PlotUtils
|
@reexport using PlotUtils
|
||||||
@reexport using PlotThemes
|
@reexport using PlotThemes
|
||||||
import Showoff
|
import Showoff
|
||||||
|
import StatsBase
|
||||||
|
|
||||||
export
|
export
|
||||||
grid,
|
grid,
|
||||||
|
|||||||
107
src/recipes.jl
107
src/recipes.jl
@ -381,51 +381,37 @@ end
|
|||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# Histograms
|
# Histograms
|
||||||
|
|
||||||
# edges from number of bins
|
function my_hist(v, bins::Int, weights::Void)
|
||||||
function calc_edges(v, bins::Integer)
|
h = StatsBase.fit(StatsBase.Histogram, v, nbins = bins)
|
||||||
vmin, vmax = extrema(v)
|
h.edges[1], h.weights
|
||||||
linspace(vmin, vmax, bins+1)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# just pass through arrays
|
function my_hist(v, bins::AVec, weights::Void)
|
||||||
calc_edges(v, bins::AVec) = bins
|
h = StatsBase.fit(StatsBase.Histogram, v, bins)
|
||||||
|
h.edges[1], h.weights
|
||||||
# find the bucket index of this value
|
|
||||||
function bucket_index(vi, edges)
|
|
||||||
for (i,e) in enumerate(edges)
|
|
||||||
if vi <= e
|
|
||||||
return max(1,i-1)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return length(edges)-1
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function my_hist(v, bins; normed = false, weights = nothing)
|
function my_hist(v, bins::Int, weights::AVec)
|
||||||
edges = calc_edges(v, bins)
|
h = StatsBase.fit(StatsBase.Histogram, v, StatsBase.weights(weights), nbins = bins)
|
||||||
counts = zeros(length(edges)-1)
|
h.edges[1], h.weights
|
||||||
|
end
|
||||||
|
|
||||||
# add a weighted count
|
function my_hist(v, bins::AVec, weights::AVec)
|
||||||
for (i,vi) in enumerate(v)
|
h = StatsBase.fit(StatsBase.Histogram, v, bins, StatsBase.weights(weights))
|
||||||
idx = bucket_index(vi, edges)
|
h.edges[1], h.weights
|
||||||
counts[idx] += (weights == nothing ? 1.0 : weights[i])
|
end
|
||||||
end
|
|
||||||
|
@recipe function f(::Type{Val{:histogram}}, x, y, z)
|
||||||
|
edges, counts = my_hist(y, d[:bins], d[:weights])
|
||||||
|
|
||||||
# normalize by bar area?
|
# normalize by bar area?
|
||||||
norm_denom = normed ? sum(diff(edges) .* counts) : 1.0
|
norm_denom = d[:normalize] ? sum(diff(edges) .* counts) : 1.0
|
||||||
if norm_denom == 0
|
if norm_denom == 0
|
||||||
norm_denom = 1.0
|
norm_denom = 1.0
|
||||||
end
|
end
|
||||||
|
|
||||||
edges, counts ./ norm_denom
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
@recipe function f(::Type{Val{:histogram}}, x, y, z)
|
|
||||||
edges, counts = my_hist(y, d[:bins],
|
|
||||||
normed = d[:normalize],
|
|
||||||
weights = d[:weights])
|
|
||||||
x := edges
|
x := edges
|
||||||
y := counts
|
y := counts ./ norm_denom
|
||||||
seriestype := :bar
|
seriestype := :bar
|
||||||
()
|
()
|
||||||
end
|
end
|
||||||
@ -434,52 +420,55 @@ end
|
|||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# Histogram 2D
|
# Histogram 2D
|
||||||
|
|
||||||
# if tuple, map out bins, otherwise use the same for both
|
centers(v::AVec) = 0.5 * (v[1:end-1] + v[2:end])
|
||||||
calc_edges_2d(x, y, bins) = calc_edges(x, bins), calc_edges(y, bins)
|
|
||||||
calc_edges_2d{X,Y}(x, y, bins::Tuple{X,Y}) = calc_edges(x, bins[1]), calc_edges(y, bins[2])
|
|
||||||
|
|
||||||
# the 2D version
|
function my_hist_2d(v1, v2, bins::Union{AVec, Tuple{AVec, AVec}}, weights::AVec)
|
||||||
function my_hist_2d(x, y, bins; normed = false, weights = nothing)
|
h = StatsBase.fit(StatsBase.Histogram, (v1, v2), bins, StatsBase.weights(weights))
|
||||||
xedges, yedges = calc_edges_2d(x, y, bins)
|
h.edges..., Float64.(h.weights)
|
||||||
counts = zeros(length(yedges)-1, length(xedges)-1)
|
end
|
||||||
|
|
||||||
# add a weighted count
|
function my_hist_2d(v1, v2, bins::Union{AVec, Tuple{AVec, AVec}}, weights::Void)
|
||||||
for i=1:length(x)
|
h = StatsBase.fit(StatsBase.Histogram, (v1, v2), bins)
|
||||||
r = bucket_index(y[i], yedges)
|
h.edges..., Float64.(h.weights)
|
||||||
c = bucket_index(x[i], xedges)
|
end
|
||||||
counts[r,c] += (weights == nothing ? 1.0 : weights[i])
|
|
||||||
end
|
|
||||||
|
|
||||||
# normalize to cubic area of the imaginary surface towers
|
function my_hist_2d(v1, v2, bins::Union{Int, Tuple{Int, Int}}, weights::Void)
|
||||||
norm_denom = normed ? sum((diff(yedges) * diff(xedges)') .* counts) : 1.0
|
h = StatsBase.fit(StatsBase.Histogram, (v1, v2), nbins = bins)
|
||||||
|
h.edges..., Float64.(h.weights)
|
||||||
|
end
|
||||||
|
|
||||||
|
function my_hist_2d(v1, v2, bins::Union{Int, Tuple{Int, Int}}, weights::AVec)
|
||||||
|
h = StatsBase.fit(StatsBase.Histogram, (v1, v2), StatsBase.weights(weights), nbins = bins)
|
||||||
|
h.edges..., Float64.(h.weights)
|
||||||
|
end
|
||||||
|
|
||||||
|
@recipe function f(::Type{Val{:histogram2d}}, x, y, z)
|
||||||
|
yedges, xedges, counts = my_hist_2d(x, y, d[:bins], d[:weights])
|
||||||
|
|
||||||
|
norm_denom = d[:normalize] ? sum((diff(yedges) * diff(xedges)') .* counts) : 1.0
|
||||||
if norm_denom == 0
|
if norm_denom == 0
|
||||||
norm_denom = 1.0
|
norm_denom = 1.0
|
||||||
end
|
end
|
||||||
|
|
||||||
xedges, yedges, counts ./ norm_denom
|
|
||||||
end
|
|
||||||
|
|
||||||
centers(v::AVec) = 0.5 * (v[1:end-1] + v[2:end])
|
|
||||||
|
|
||||||
@recipe function f(::Type{Val{:histogram2d}}, x, y, z)
|
|
||||||
xedges, yedges, counts = my_hist_2d(x, y, d[:bins],
|
|
||||||
normed = d[:normalize],
|
|
||||||
weights = d[:weights])
|
|
||||||
for (i,c) in enumerate(counts)
|
for (i,c) in enumerate(counts)
|
||||||
if c == 0
|
if c == 0
|
||||||
counts[i] = NaN
|
counts[i] = NaN
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
x := centers(xedges)
|
x := centers(xedges)
|
||||||
y := centers(yedges)
|
y := centers(yedges)
|
||||||
z := Surface(counts)
|
z := Surface(counts ./ norm_denom)
|
||||||
linewidth := 0
|
linewidth := 0
|
||||||
seriestype := :heatmap
|
seriestype := :heatmap
|
||||||
()
|
()
|
||||||
end
|
end
|
||||||
@deps histogram2d heatmap
|
@deps histogram2d heatmap
|
||||||
|
|
||||||
|
@recipe function f{T, E}(h::StatsBase.Histogram{T, 2, E})
|
||||||
|
seriestype := :bar
|
||||||
|
h.edges[1], h.weights
|
||||||
|
end
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# scatter 3d
|
# scatter 3d
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user