Skip to content

Commit

Permalink
major update: rewrite imagesc parameters, colorbar, axis, cgap
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed Oct 22, 2024
1 parent c3d448c commit 9c680ff
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 31 deletions.
4 changes: 3 additions & 1 deletion src/Layers/Base.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export gap!
export bind_text!, bind_z!, add_labels!, text_rel!
export rm_ticks!, non_label_ticks
export rm_ticks!, non_label_ticks, format_ticks
export set_lims!


format_ticks(ticks) = collect(ticks), string.(ticks)

function Base.size(fig::Figure)
m, n = collect(fig.scene.viewport[].widths)
println("size=($m, $n)")
Expand Down
24 changes: 24 additions & 0 deletions src/Layers/add_Labels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,29 @@ function add_col_labels!(fig, labels; fontsize=14, gap=5, kw...)
plts
end

function add_ylabel!(fig, ylabel; width=5, kw...)
ylabel == "" && return
Label(fig[:, 0], ylabel;
rotation=pi / 2, tellwidth=true, tellheight=false, font=:bold, width, kw...)
end

function add_xlabel!(fig, xlabel; height=5, kw...)
xlabel == "" && return
Label(fig[end+1, :], xlabel;
tellwidth=false, tellheight=true, font=:bold, height, kw...)
end

function add_title!(fig, title; height=5, kw...)
title == "" && return
Label(fig[0, :], title;
tellwidth=false, tellheight=true, font=:bold, height, kw...)
end

function labs!(fig; xlabel="", ylabel="", title="", height=5, kw...)
add_ylabel!(fig, ylabel; width=height, kw...)
add_xlabel!(fig, xlabel; height, kw...)
add_title!(fig, title; height, kw...)
end

export add_xlabel!, add_ylabel!, add_title!, labs!
export add_col_labels!, add_row_labels!
65 changes: 41 additions & 24 deletions src/Layers/imagesc.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export set_colgap
# TODO: add base maps foreach axis

"""
Expand All @@ -19,18 +20,20 @@ Heatmap with colorbar
range is determined by the minimum and maximum values of `z`. If `colorrange`
is a tuple `(vmin, vmax)`, the range is set to `[vmin, vmax]`.
- `force_show_legend`: if `true`, the colorbar is always shown.
- `col_rev`: if `true`, the colormap is reversed.
- `colors`: the colormap. It can be a string or a vector of colors.
- `kw...`: other keyword arguments passed to `heatmap!`
- `kw_axis`: other keyword arguments passed to `Axis`
- `fun_axis` used to tidy axis
- `col_rev` : if `true`, the colormap is reversed.
- `colors` : the colormap. It can be a string or a vector of colors.
- `kw...` : other keyword arguments passed to `heatmap!`
- `kw_axis` : other keyword arguments passed to `Axis`
- `fun_axis` : used to tidy axis
- `cgap` : the gap between the colorbar and the plot (default is 5)
- `gap` : the gap between subplots (default is [10, 10])
# Examples
```julia
imagesc(rand(10, 10))
```
"""
function imagesc!(ax::Axis, x, y, z::Union{R,Observable{R}};
function _imagesc(ax::Axis, x, y, z::Union{R,Observable{R}};
colorrange=automatic, col_rev=false, colors=amwg256,
fact=nothing, kw...) where {R<:AbstractArray{<:Real,2}}

Expand All @@ -53,14 +56,21 @@ end

function imagesc!(fig::Union{Figure,GridPosition,GridSubposition},
x, y, z::Union{R,Observable{R}};
title="Plot",
title="",
force_show_legend=false,
colorrange=automatic, kw_axis=(;), kw...) where {R<:AbstractArray{<:Real,2}}

ax = Axis(fig[1, 1]; title, kw_axis...)
plt = imagesc!(ax, x, y, z; colorrange, kw...)

(colorrange == automatic || force_show_legend) && Colorbar(fig[1, 2], plt)
colorrange=automatic,
axis=(;),
colorbar=(; width=20),
cgap=5,
kw...) where {R<:AbstractArray{<:Real,2}}

ax = Axis(fig[1, 1]; title, axis...)
plt = _imagesc(ax, x, y, z; colorrange, kw...)

if (colorrange == automatic || force_show_legend)
Colorbar(fig[1, 2], plt; colorbar...)
!isnothing(cgap) && set_colgap(fig, 1, cgap)
end
ax, plt
end

Expand All @@ -69,14 +79,15 @@ function imagesc!(fig::Union{Figure,GridPosition,GridSubposition},
x, y, z::Union{R,Observable{R}};
colorrange=automatic, force_show_legend=false,
layout=nothing,
titles=nothing, colors=amwg256, gap=10,
kw_cbar=(;),
cbar_width=15,
titles=nothing, colors=amwg256,
xlabel=nothing, ylabel=nothing, title=nothing,
gap=10, cgap=5,
axis=(;), colorbar=(; width=15),
fun_axis=nothing,
byrow=false,
kw...) where {R<:AbstractArray{<:Real,3}}

length(gap) == 1 && (gap = (gap, gap, 5))
length(gap) == 1 && (gap = (gap, gap))
n = size(z, 3)
if layout === nothing
ncol = ceil(Int, sqrt(n))
Expand All @@ -90,7 +101,7 @@ function imagesc!(fig::Union{Figure,GridPosition,GridSubposition},
plts = []

k = 0
inds = byrow ? CartesianIndices((1:ncol, 1:nrow)) : CartesianIndices((1:nrow, 1:ncol))
inds = byrow ? CartesianIndices((1:ncol, 1:nrow)) : CartesianIndices((1:nrow, 1:ncol))

for I in inds[1:n]
if byrow
Expand All @@ -107,7 +118,8 @@ function imagesc!(fig::Union{Figure,GridPosition,GridSubposition},
_z = z[:, :, k]
end
ax, plt = imagesc!(fig[i, j], x, y, _z;
title, colorrange, force_show_legend, colors, kw...)
title, colorrange, force_show_legend, colors,
axis, colorbar, cgap, kw...)
fun_axis !== nothing && fun_axis(ax)
push!(axs, ax)
push!(plts, plt)
Expand All @@ -122,16 +134,21 @@ function imagesc!(fig::Union{Figure,GridPosition,GridSubposition},
cbar = nothing
# unify the legend
if colorrange != automatic && !force_show_legend
cbar = Colorbar(fig[1:nrow, ncol+1], plts[1]; width=cbar_width, kw_cbar...)
set_colgap(fig, ncol, gap[3])
cbar = Colorbar(fig[1:nrow, ncol+1], plts[1]; colorbar...)
set_colgap(fig, ncol, cgap)
end
axs, plts, cbar
end

set_colgap(fig::Figure, j, gap) = colgap!(fig.layout, j, gap)
function set_colgap(fig::GridPosition, j, gap)
layout = fig.layout.content[1].content
colgap!(layout, j, gap)
contents = fig.layout.content
# for i in 1:length(contents)
_content = contents[end].content
colgap!(_content, j, gap)
# end
# layout = fig.layout.content[1].content
# colgap!(layout, j, gap)
end

function imagesc!(fig, z; kw...)
Expand All @@ -142,6 +159,6 @@ end

function imagesc(x, args...; kw...)
fig = Figure()
imagesc!(fig, x, args...; kw...)
R = imagesc!(fig, x, args...; kw...)
fig
end
3 changes: 3 additions & 0 deletions test/main_pkgs.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# using GLMakie, MakieLayers
using CairoMakie, MakieLayers
using Test, Dates
10 changes: 4 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using Test
using Dates
using MakieLayers
using CairoMakie
# using GLMakie
include("main_pkgs.jl")

include("test-colorbar.jl")
include("test-colors.jl")

@testset "dateseries" begin
## good job, test passed
Expand Down Expand Up @@ -39,7 +37,7 @@ end
@test_nowarn imagesc(x, y, rand(10, 10, 4))

fig = Figure(; size = (800, 800))
@test_nowarn imagesc!(fig, x, y, rand(10, 10, 4), colorrange=(0, 1), kw_cbar=(;width=45))
@test_nowarn imagesc!(fig, x, y, rand(10, 10, 4), colorrange=(0, 1), colorbar=(;width=45))
add_row_labels!(fig, ["(a)", "(b)"])
add_col_labels!(fig, ["1", "2"])
fig
Expand Down
49 changes: 49 additions & 0 deletions test/test-colorbar.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
include("main_pkgs.jl")
## 该脚本用于说明,如何设置axis and colorbar properties

t = 0.5:0.5:24
nhour = length(t)
nday = 100

yticks = 0:3:24 |> format_ticks
xticks = 0:20:100 |> format_ticks
ticks = -0.5:0.1:0.5 |> format_ticks

x = 1:nday
y = t
z = rand(nday, nhour, 2)

# 2d array passed
begin
fig = Figure(; size=(600, 600))
ax, plt = imagesc!(fig, x, y, z[:, :, 1];
colorbar=(; ticks, width=30), cgap=5,
axis=(; xticks, yticks, xlabel="Days", ylabel="Hours"), title="")
fig
end

## 3d array passed perfectly
z = rand(nhour, nday, 2)
imagesc(x, y, z;
colorbar=(; ticks, width=30), cgap=5,
axis=(; xticks, yticks, xlabel="Days", ylabel="Hours"))

## 3d array, 统一colorbar
imagesc(x, y, z;
colorrange=(0, 1),
colorbar=(; ticks, width=30), cgap=15,
axis=(; xticks, yticks, xlabel="Days", ylabel="Hours"))

## 测试xlab and ylab
begin
fig = imagesc(x, y, z;
colorrange=(0, 1),
colorbar=(; ticks, width=30), cgap=15, gap=(10, 10),
axis=(; xticks, yticks))
# xlabel = "Days", ylabel = "Hours"
labs!(fig; xlabel="Days", ylabel="Hours")
add_title!(fig, "Title"; fontsize=24, height=15)
# add_xlabel!(fig, "Days")
# add_ylabel!(fig, "Hours")
fig
end
16 changes: 16 additions & 0 deletions test/test-colors.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
include("main_pkgs.jl")

## 离散型变量colorbar
z = round.(Int, rand(10, 10) * 10)

colorrange = (0, 10)
nbrk = colorrange[2] - colorrange[1] + 1
# 首尾 + (-0.5, 0.5),让ticks居中

cols = resample_colors(amwg256, 12)
colors = cgrad(cols, nbrk, categorical=true)

ticks = 0:10 |> format_ticks
imagesc(z; colors,
colorbar = (; ticks, width=30), cgap=5,
colorrange=colorrange .+ (-0.5, 0.5), force_show_legend=true)

0 comments on commit 9c680ff

Please sign in to comment.