Merge 148287402e076e4cd683ce49a311958c1256392e into 7b1941b12926730092b34fafef88cad40fadf3e2

This commit is contained in:
BerndR 2021-08-01 21:58:35 +02:00 committed by GitHub
commit d6bb07b7e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 139 additions and 4 deletions

View File

@ -51,12 +51,12 @@ const _axesAliases = Dict{Symbol,Symbol}(
) )
const _3dTypes = [ const _3dTypes = [
:path3d, :scatter3d, :surface, :wireframe, :contour3d, :volume, :mesh3d :path3d, :scatter3d, :surface, :wireframe, :contour3d, :volume, :mesh3d, :histogram3d
] ]
const _allTypes = vcat([ const _allTypes = vcat([
:none, :line, :path, :steppre, :stepmid, :steppost, :sticks, :scatter, :none, :line, :path, :steppre, :stepmid, :steppost, :sticks, :scatter,
:heatmap, :hexbin, :barbins, :barhist, :histogram, :scatterbins, :heatmap, :hexbin, :barbins, :barhist, :histogram, :scatterbins,
:scatterhist, :stepbins, :stephist, :bins2d, :histogram2d, :histogram3d, :scatterhist, :stepbins, :stephist, :bins2d, :histogram2d,
:density, :bar, :hline, :vline, :density, :bar, :hline, :vline,
:contour, :pie, :shape, :image :contour, :pie, :shape, :image
], _3dTypes) ], _3dTypes)
@ -94,13 +94,15 @@ const _typeAliases = Dict{Symbol,Symbol}(
:imagesc => :image, :imagesc => :image,
:hist => :histogram, :hist => :histogram,
:hist2d => :histogram2d, :hist2d => :histogram2d,
:hist3d => :histogram3d,
:lego => :histogram3d,
:bezier => :curves, :bezier => :curves,
:bezier_curves => :curves, :bezier_curves => :curves,
) )
add_non_underscore_aliases!(_typeAliases) add_non_underscore_aliases!(_typeAliases)
const _histogram_like = [:histogram, :barhist, :barbins] const _histogram_like = [:histogram, :barhist, :barbins, :histogram3d]
const _line_like = [:line, :path, :steppre, :stepmid, :steppost] const _line_like = [:line, :path, :steppre, :stepmid, :steppost]
const _surface_like = [:contour, :contourf, :contour3d, :heatmap, :surface, :wireframe, :image] const _surface_like = [:contour, :contourf, :contour3d, :heatmap, :surface, :wireframe, :image]

View File

@ -521,7 +521,7 @@ function widen(lmin, lmax, scale=:identity)
end end
# figure out if widening is a good idea. # figure out if widening is a good idea.
const _widen_seriestypes = (:line, :path, :steppre, :stepmid, :steppost, :sticks, :scatter, :barbins, :barhist, :histogram, :scatterbins, :scatterhist, :stepbins, :stephist, :bins2d, :histogram2d, :bar, :shape, :path3d, :scatter3d) const _widen_seriestypes = (:line, :path, :steppre, :stepmid, :steppost, :sticks, :scatter, :barbins, :barhist, :histogram, :scatterbins, :scatterhist, :stepbins, :stephist, :bins2d, :histogram2d, :bar, :shape, :path3d, :scatter3d, :histogram3d)
function default_should_widen(axis::Axis) function default_should_widen(axis::Axis)
if axis[:widen] isa Bool if axis[:widen] isa Bool

View File

@ -987,6 +987,118 @@ end
end end
# ---------------------------------------------------------------------------
# Histogram 3D
# Check if all neighbours have the same value
# TODO: Improve this somehow
function _allneighbours(arr, I::CartesianIndex, val = 0.0)
arr[I] == val &&
arr[I + CartesianIndex(1,0)] == val &&
arr[I + CartesianIndex(0,1)] == val &&
arr[I - CartesianIndex(1,0)] == val &&
arr[I - CartesianIndex(0,1)] == val &&
arr[I + CartesianIndex(1,1)] == val &&
arr[I + CartesianIndex(1,-1)] == val &&
arr[I - CartesianIndex(1,1)] == val &&
arr[I - CartesianIndex(1,-1)] == val
end
@recipe function f(::Type{Val{:bins3d}}, x, y, z)
edge_x, edge_y, weights = x, y, z.surf
# Create the bins
if !plotattributes[:show_empty_bins]
edge_x = repeat(edge_x,inner=(4))[2:end-1]
edge_y = repeat(edge_y,inner=(4))[2:end-1]
float_weights = spzeros(length(edge_x),length(edge_y))
float_weights[2:end-1,2:end-1] = repeat(float(weights),inner=(4,4))
m = falses(size(float_weights))
for I in CartesianIndices((2:length(edge_x)-1,2:length(edge_y)-1))
m[I] = _allneighbours(float_weights,I,0)
end
float_weights[m] .= NaN
else
edge_x = repeat(edge_x,inner=(2))
edge_y = repeat(edge_y,inner=(2))
float_weights = spzeros(length(edge_x),length(edge_y))
float_weights[2:end-1,2:end-1] .= repeat(float(weights),inner=(2,2))
end
# pyplot can't handle sparse arrays it seems
# Maybe get rid off sparse arrays at all, but if any bins-entry is large the additional points for plotting may use up a lot of memory otherwise for zeros and NaNs
if backend_name() == :pyplot
float_weights = Surface(permutedims(Array(float_weights)))
else
float_weights = Surface(permutedims(float_weights))
end
# Handle input of one seriescolor only (maybe exchangeable by get_series_color()?)
if isa(plotattributes[:seriescolor], Symbol) && plotattributes[:seriescolor] != :auto
seriescolor := cgrad([plotattributes[:seriescolor], plotattributes[:seriescolor]])
end
seriestype := :surface
colorbar := false
x := edge_x
y := edge_y
z := float_weights
()
end
Plots.@deps bins3d surface #wireframe mesh3d
@recipe function f(::Type{Val{:bricks3d}}, x, y, z)
edge_x, edge_y, weights = x, y, z.surf
x_step = edge_x[2] - edge_x[1]
y_step = edge_y[2] - edge_y[1]
x_len = length(x)
y_len = length(y)
temp_x = vec([0.0 0.0 1.0 1.0]).*x_step .+ edge_x'
temp_y = vec([1.0 1.0 0.0 0.0]).*y_step .+ edge_y'
z_help = [1.0 0.0 0.0 1.0;
0.0 1.0 1.0 0.0;
0.0 1.0 1.0 0.0;
1.0 0.0 0.0 1.0];
for (i, c) in enumerate(weights)
itx = (i-1)%y_len + 1
ity = floor(Int64,i/x_len) + 1
@series begin
seriestype := :surface
x := temp_x[:,itx]
y := temp_y[:,ity]
z := c == 0 ? Surface(permutedims(NaN .* z_help)) : Surface(permutedims(c .* z_help))
()
end
end
end
Plots.@deps bins3d surface #wireframe mesh3d
@recipe function f(::Type{Val{:histogram3d}}, x, y, z)
h = _make_hist(
(x, y),
plotattributes[:bins],
normed = plotattributes[:normalize],
weights = plotattributes[:weights],
)
x := h.edges[1]
y := h.edges[2]
z := Surface(h.weights)
seriestype := :bricks3d#:bins3d
()
end
@deps histogram3d bins3d bricks3d
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# pie # pie
@recipe function f(::Type{Val{:pie}}, x, y, z) @recipe function f(::Type{Val{:pie}}, x, y, z)

View File

@ -107,6 +107,26 @@ julia> histogram2d(randn(10_000),randn(10_000))
""" """
@shorthands histogram2d @shorthands histogram2d
"""
histogram3d(x,y)
histogram3d!(x,y)
Plot a three-dimensional histogram.
# Arguments
- `bins`: Number of bins (if an `Integer`) or bin edges (if an `AbtractVector`)
- `weights`: Vector of weights for the values in `x`. Each entry of x contributes
its weight to the height of its bin.
# Example
```julia-repl
julia> histogram3d(randn(10_000),randn(10_000))
```
"""
@shorthands histogram3d
@shorthands legoplot
""" """
density(x) density(x)
density!(x) density!(x)

View File

@ -49,6 +49,7 @@ function should_add_to_legend(series::Series)
:hexbin, :hexbin,
:bins2d, :bins2d,
:histogram2d, :histogram2d,
:histogram3d,
:hline, :hline,
:vline, :vline,
:contour, :contour,