diff --git a/src/Layers/Base.jl b/src/Layers/Base.jl index b097f92..b945b02 100644 --- a/src/Layers/Base.jl +++ b/src/Layers/Base.jl @@ -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)") diff --git a/src/Layers/add_Labels.jl b/src/Layers/add_Labels.jl index 8538b48..a0031a2 100644 --- a/src/Layers/add_Labels.jl +++ b/src/Layers/add_Labels.jl @@ -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! diff --git a/src/Layers/imagesc.jl b/src/Layers/imagesc.jl index 669ffaf..7f071fb 100644 --- a/src/Layers/imagesc.jl +++ b/src/Layers/imagesc.jl @@ -1,3 +1,4 @@ +export set_colgap # TODO: add base maps foreach axis """ @@ -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}} @@ -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 @@ -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)) @@ -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 @@ -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) @@ -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...) @@ -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 diff --git a/test/main_pkgs.jl b/test/main_pkgs.jl new file mode 100644 index 0000000..06658ba --- /dev/null +++ b/test/main_pkgs.jl @@ -0,0 +1,3 @@ +# using GLMakie, MakieLayers +using CairoMakie, MakieLayers +using Test, Dates diff --git a/test/runtests.jl b/test/runtests.jl index ef3cefc..cdd9303 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 @@ -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 diff --git a/test/test-colorbar.jl b/test/test-colorbar.jl new file mode 100644 index 0000000..e03f2a4 --- /dev/null +++ b/test/test-colorbar.jl @@ -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 diff --git a/test/test-colors.jl b/test/test-colors.jl new file mode 100644 index 0000000..3cd3a09 --- /dev/null +++ b/test/test-colors.jl @@ -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)