added density recipe

This commit is contained in:
Thomas Breloff 2016-06-07 00:18:07 -04:00
parent 6bfa31a89f
commit 60d97ad9c9
4 changed files with 35 additions and 14 deletions

View File

@ -174,7 +174,7 @@ supportedMarkers(::AbstractBackend) = [:none]
supportedScales(::AbstractBackend) = [:identity]
subplotSupported(::AbstractBackend) = false
stringsSupported(::AbstractBackend) = false
nativeImagesSupported(::AbstractBackend) = false
nativeImagesSupported(b::AbstractBackend) = :image in supportedTypes(b)
supportedAxes() = supportedAxes(backend())
supportedTypes() = supportedTypes(backend())

View File

@ -52,7 +52,7 @@ supportedStyles(::GRBackend) = [:auto, :solid, :dash, :dot, :dashdot, :dashdotdo
supportedMarkers(::GRBackend) = vcat(_allMarkers, Shape)
supportedScales(::GRBackend) = [:identity, :log10]
subplotSupported(::GRBackend) = true
nativeImagesSupported(::GRBackend) = true
# nativeImagesSupported(::GRBackend) = true
@ -722,14 +722,11 @@ function gr_display(sp::Subplot{GRBackend}, w, h, viewport_canvas)
GR.settextcolorind(gr_getcolorind(xaxis[:foreground_color_text]))
if axes_2d
# draw the grid lines
GR.setlinewidth(1)
# GR.setlinetype(GR.LINETYPE_DOTTED)
GR.setlinecolorind(gr_getcolorind(sp[:foreground_color_grid]))
ticksize = 0.0075 * window_diag
if outside_ticks
ticksize = -ticksize
end
if grid_flag
GR.setlinecolorind(gr_getcolorind(sp[:foreground_color_grid]))
GR.grid(xtick, ytick, 0, 0, majorx, majory)
# @show dark_bg, xtick, ytick, majorx, majory
# if dark_bg
@ -738,6 +735,11 @@ function gr_display(sp::Subplot{GRBackend}, w, h, viewport_canvas)
# GR.grid(xtick, ytick, 0, 0, majorx, majory)
# end
end
ticksize = 0.0075 * window_diag
if outside_ticks
ticksize = -ticksize
end
# TODO: this should be done for each axis separately
GR.setlinecolorind(gr_getcolorind(xaxis[:foreground_color_axis]))
if num_axes == 1

View File

@ -49,7 +49,7 @@ supportedStyles(::PyPlotBackend) = [:auto, :solid, :dash, :dot, :dashdot]
supportedMarkers(::PyPlotBackend) = vcat(_allMarkers, Shape)
supportedScales(::PyPlotBackend) = [:identity, :ln, :log2, :log10]
subplotSupported(::PyPlotBackend) = true
nativeImagesSupported(::PyPlotBackend) = true
# nativeImagesSupported(::PyPlotBackend) = true
# --------------------------------------------------------------------------------------

View File

@ -361,12 +361,14 @@ function my_hist(v, bins; normed = false, weights = nothing)
edges = calc_edges(v, bins)
counts = zeros(length(edges)-1)
# add a weighted count
for (i,vi) in enumerate(v)
idx = bucket_index(vi, edges)
counts[idx] += (weights == nothing ? 1.0 : weights[i])
end
norm_denom = normed ? sum(counts) : 1.0
# normalize by bar area?
norm_denom = normed ? sum(diff(edges) .* counts) : 1.0
if norm_denom == 0
norm_denom = 1.0
end
@ -395,13 +397,15 @@ function my_hist_2d(x, y, bins; normed = false, weights = nothing)
xedges, yedges = calc_edges_2d(x, y, bins)
counts = zeros(length(yedges)-1, length(xedges)-1)
# add a weighted count
for i=1:length(x)
r = bucket_index(y[i], yedges)
c = bucket_index(x[i], xedges)
counts[r,c] += (weights == nothing ? 1.0 : weights[i])
end
norm_denom = normed ? sum(counts) : 1.0
# normalize to cubic area of the imaginary surface towers
norm_denom = normed ? sum((diff(yedges) * diff(xedges)') .* counts) : 1.0
if norm_denom == 0
norm_denom = 1.0
end
@ -477,13 +481,13 @@ end
# otherwise, just use a histogram
if is_installed("KernelDensity")
@eval import KernelDensity
@eval function violin_coords(y)
kd = KernelDensity.kde(y, npoints = 30)
@eval function violin_coords(y, bins = 30)
kd = KernelDensity.kde(y, npoints = isa(bins, Integer) ? bins : 30)
kd.density, kd.x
end
else
@eval function violin_coords(y)
edges, widths = hist(y, 20)
@eval function violin_coords(y, bins = 30)
edges, widths = hist(y, isa(bins, Integer) ? bins : 30)
centers = 0.5 * (edges[1:end-1] + edges[2:end])
ymin, ymax = extrema(y)
vcat(0.0, widths, 0.0), vcat(ymin, centers, ymax)
@ -526,6 +530,21 @@ end
# KW[d]
end
# ---------------------------------------------------------------------------
# density
@recipe function f(::Type{Val{:density}}, x, y, z)
newx, newy = violin_coords(y, d[:bins])
if isvertical(d)
newx, newy = newy, newx
end
d[:x], d[:y] = newx, newy
seriestype := :path
()
end
# ---------------------------------------------------------------------------
# Error Bars