Merge pull request #2234 from lmh91/lmh_gr_nonuniform_heatmap
Nonuniform heatmaps are now possible with the GR backend.
This commit is contained in:
commit
63cd86589c
@ -920,11 +920,8 @@ function gr_display(sp::Subplot{GRBackend}, w, h, viewport_canvas)
|
|||||||
outside_ticks = true
|
outside_ticks = true
|
||||||
for ax in (sp[:xaxis], sp[:yaxis])
|
for ax in (sp[:xaxis], sp[:yaxis])
|
||||||
v = series[ax[:letter]]
|
v = series[ax[:letter]]
|
||||||
if length(v) > 1 && diff(collect(extrema(diff(v))))[1] > 1e-6*std(v)
|
|
||||||
@warn("GR: heatmap only supported with equally spaced data.")
|
|
||||||
end
|
end
|
||||||
end
|
x, y = heatmap_edges(series[:x], sp[:xaxis][:scale], series[:y], sp[:yaxis][:scale], size(series[:z]))
|
||||||
x, y = heatmap_edges(series[:x], sp[:xaxis][:scale]), heatmap_edges(series[:y], sp[:yaxis][:scale])
|
|
||||||
xy_lims = x[1], x[end], y[1], y[end]
|
xy_lims = x[1], x[end], y[1], y[end]
|
||||||
expand_extrema!(sp[:xaxis], x)
|
expand_extrema!(sp[:xaxis], x)
|
||||||
expand_extrema!(sp[:yaxis], y)
|
expand_extrema!(sp[:yaxis], y)
|
||||||
@ -1317,23 +1314,31 @@ function gr_display(sp::Subplot{GRBackend}, w, h, viewport_canvas)
|
|||||||
elseif st == :heatmap
|
elseif st == :heatmap
|
||||||
zmin, zmax = clims
|
zmin, zmax = clims
|
||||||
if !ispolar(sp)
|
if !ispolar(sp)
|
||||||
xmin, xmax, ymin, ymax = xy_lims
|
|
||||||
m, n = length(x), length(y)
|
|
||||||
GR.setspace(zmin, zmax, 0, 90)
|
GR.setspace(zmin, zmax, 0, 90)
|
||||||
grad = isa(series[:fillcolor], ColorGradient) ? series[:fillcolor] : cgrad()
|
x, y = heatmap_edges(series[:x], sp[:xaxis][:scale], series[:y], sp[:yaxis][:scale], size(series[:z]))
|
||||||
colors = [plot_color(grad[clamp((zi-zmin) / (zmax-zmin), 0, 1)], series[:fillalpha]) for zi=z]
|
w, h = length(x) - 1, length(y) - 1
|
||||||
rgba = map(c -> UInt32( round(UInt, alpha(c) * 255) << 24 +
|
z_normalized = map(x -> GR.jlgr.normalize_color(x, zmin, zmax), z)
|
||||||
round(UInt, blue(c) * 255) << 16 +
|
z_normalized = map(x -> isnan(x) ? 256/255 : x, z_normalized) # results in color index = 1256 -> transparent
|
||||||
round(UInt, green(c) * 255) << 8 +
|
colors = Int32[round(Int32, 1000 + _i * 255) for _i in z_normalized]
|
||||||
round(UInt, red(c) * 255) ), colors)
|
GR.nonuniformcellarray(x, y, w, h, colors)
|
||||||
w, h = length(x), length(y)
|
|
||||||
GR.drawimage(xmin, xmax, ymax, ymin, w, h, rgba)
|
|
||||||
else
|
else
|
||||||
h, w = length(x), length(y)
|
phimin, phimax = 0.0, 360.0 # nonuniform polar array is not yet supported in GR.jl
|
||||||
z = reshape(z, h, w)
|
nx, ny = length(series[:x]), length(series[:y])
|
||||||
colors = Int32[round(Int32, 1000 + _i * 255) for _i in z']
|
z_normalized = map(x -> GR.jlgr.normalize_color(x, zmin, zmax), z)
|
||||||
GR.setwindow(-1, 1, -1, 1)
|
colors = Int32[round(Int32, 1000 + _i * 255) for _i in z_normalized]
|
||||||
GR.polarcellarray(0, 0, 0, 360, 0, 1, w, h, colors)
|
xmin, xmax, ymin, ymax = xy_lims
|
||||||
|
rmax = data_lims[4]
|
||||||
|
GR.setwindow(-rmax, rmax, -rmax, rmax)
|
||||||
|
if ymin > 0
|
||||||
|
@warn "'ymin[1] > 0' (rmin) is not yet supported."
|
||||||
|
end
|
||||||
|
if series[:y][end] != ny
|
||||||
|
@warn "Right now only the maximum value of y (r) is taken into account."
|
||||||
|
end
|
||||||
|
# GR.polarcellarray(0, 0, phimin, phimax, ymin, ymax, nx, ny, colors)
|
||||||
|
GR.polarcellarray(0, 0, phimin, phimax, 0, ymax, nx, ny, colors)
|
||||||
|
# Right now only the maximum value of y (r) is taken into account.
|
||||||
|
# This is certainly not perfect but nonuniform polar array is not yet supported in GR.jl
|
||||||
end
|
end
|
||||||
|
|
||||||
elseif st in (:path3d, :scatter3d)
|
elseif st in (:path3d, :scatter3d)
|
||||||
|
|||||||
25
src/utils.jl
25
src/utils.jl
@ -342,8 +342,11 @@ const _scale_base = Dict{Symbol, Real}(
|
|||||||
:ln => ℯ,
|
:ln => ℯ,
|
||||||
)
|
)
|
||||||
|
|
||||||
function _heatmap_edges(v::AVec)
|
function _heatmap_edges(v::AVec, isedges::Bool = false)
|
||||||
length(v) == 1 && return v[1] .+ [-0.5, 0.5]
|
length(v) == 1 && return v[1] .+ [-0.5, 0.5]
|
||||||
|
if isedges return v end
|
||||||
|
# `isedges = true` means that v is a vector which already describes edges
|
||||||
|
# and does not need to be extended.
|
||||||
vmin, vmax = ignorenan_extrema(v)
|
vmin, vmax = ignorenan_extrema(v)
|
||||||
extra_min = (v[2] - v[1]) / 2
|
extra_min = (v[2] - v[1]) / 2
|
||||||
extra_max = (v[end] - v[end - 1]) / 2
|
extra_max = (v[end] - v[end - 1]) / 2
|
||||||
@ -351,9 +354,25 @@ function _heatmap_edges(v::AVec)
|
|||||||
end
|
end
|
||||||
|
|
||||||
"create an (n+1) list of the outsides of heatmap rectangles"
|
"create an (n+1) list of the outsides of heatmap rectangles"
|
||||||
function heatmap_edges(v::AVec, scale::Symbol = :identity)
|
function heatmap_edges(v::AVec, scale::Symbol = :identity, isedges::Bool = false)
|
||||||
f, invf = scalefunc(scale), invscalefunc(scale)
|
f, invf = scalefunc(scale), invscalefunc(scale)
|
||||||
map(invf, _heatmap_edges(map(f,v)))
|
map(invf, _heatmap_edges(map(f,v), isedges))
|
||||||
|
end
|
||||||
|
|
||||||
|
function heatmap_edges(x::AVec, xscale::Symbol, y::AVec, yscale::Symbol, z_size::Tuple{Int, Int})
|
||||||
|
nx, ny = length(x), length(y)
|
||||||
|
# ismidpoints = z_size == (ny, nx) # This fails some tests, but would actually be
|
||||||
|
# the correct check, since (4, 3) != (3, 4) and a missleading plot is produced.
|
||||||
|
ismidpoints = prod(z_size) == (ny * nx)
|
||||||
|
isedges = z_size == (ny - 1, nx - 1)
|
||||||
|
if !ismidpoints && !isedges
|
||||||
|
error("""Length of x & y does not match the size of z.
|
||||||
|
Must be either `size(z) == (length(y), length(x))` (x & y define midpoints)
|
||||||
|
or `size(z) == (length(y)+1, length(x)+1))` (x & y define edges).""")
|
||||||
|
end
|
||||||
|
x, y = heatmap_edges(x, xscale, isedges),
|
||||||
|
heatmap_edges(y, yscale, isedges)
|
||||||
|
return x, y
|
||||||
end
|
end
|
||||||
|
|
||||||
function convert_to_polar(theta, r, r_extrema = ignorenan_extrema(r))
|
function convert_to_polar(theta, r, r_extrema = ignorenan_extrema(r))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user