Skip to content

Commit

Permalink
improve imagesc
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed Apr 3, 2024
1 parent ca211c9 commit a9514b8
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 24 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ Manifest.toml
.vscode

*debug*
shp

scripts
example
7 changes: 7 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
Colors = "0.12"
ColorSchemes = "3"
Makie = "0.20, 0.21"
Shapefile = "0.12"
PlotUtils = "1"
julia = "1.9, 1.10"

Expand All @@ -24,3 +25,9 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "CairoMakie"]

[extensions]
MakieLayersShapefileExt = "Shapefile"

[weakdeps]
Shapefile = "8e980c4a-a4fe-5da2-b3a7-4b4b0353a2f4"
39 changes: 39 additions & 0 deletions ext/MakieLayersShapefileExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
export MakieLayersShapefileExt
module MakieLayersShapefileExt

using Shapefile
import Makie
import Makie: Axis, Point2f0, Point2f, Polygon, Poly, poly!, convert_arguments

# using MakieLayers
import MakieLayers: read_sf, plot_poly!

read_sf(f) = Shapefile.Table(f) #|> DataFrame

# https://discourse.julialang.org/t/best-way-of-handling-shapefiles-in-makie/71028/9
function Makie.convert_arguments(::Type{<:Poly}, p::Shapefile.Polygon)
# this is inefficient because it creates an array for each point
polys = Shapefile.GeoInterface.coordinates(p)
ps = map(polys) do pol
Polygon(
Point2f0.(pol[1]), # interior
map(x -> Point2f.(x), pol[2:end]))
end
(ps,)
end

"""
plot_poly!(ax::Axis, shp::Shapefile.Table;
color=nan_color, strokewidth=0.7, strokecolor=:black, kw...)
"""
function plot_poly!(ax::Axis, shp::Shapefile.Table;
color=nan_color, strokewidth=0.7, strokecolor=:black, kw...)

foreach(shp.geometry) do geo
ismissing(geo) && return
poly!(ax, geo; color, strokewidth, strokecolor, kw...)
end
end


end
40 changes: 25 additions & 15 deletions src/Layers/dateseries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,30 @@ using PlotUtils: optimize_ticks, optimize_datetime_ticks
date2num = datetime2unix

# datetime2julian, datetime2rata, datetime2unix
function guess_date_ticks(dates; fmt="mm/dd")
function get_date_ticks(dates; fmt="mm/dd")
dateticks = optimize_ticks(dates[1], dates[end])[1]
(date2num.(dateticks), Dates.format.(dateticks, fmt))
end

## How to update ticks for Dates
function get_ticks(t::AbstractVector{DateTime}, any_scale, ::Automatic, vmin, vmax; kw...)
guess_date_ticks(t; kw...)
end
# ## How to update ticks for Dates
# function get_ticks(t::AbstractVector{DateTime}, any_scale, ::Automatic, vmin, vmax; kw...)
# get_date_ticks(t; kw...)
# end

function set_date_ticks!(ax, t::AbstractVector{<:TypeDate};
date_format="yy/mm/dd")

xlims = date2num.(extrema(t))
xlims!(ax, xlims)
## update ticks and xticklabels
xticks = get_date_ticks(t; fmt=date_format)
ax.xticks[] = xticks
end

## layer: dateseries! ----------------------------------------------------------
@recipe(DateSeries, curves) do scene
attrs = default_theme(scene, Series)
return Attributes(;attrs...)
return Attributes(; attrs...)
end

Makie.convert_arguments(::Type{<:DateSeries}, args...) = convert_arguments(Series, args...)
Expand All @@ -36,19 +45,20 @@ end


function dateseries!(ax, t::Observable{<:AbstractVector{<:TypeDate}}, y;
date_format = "mm/dd", kw...)
date_format="yy/mm/dd", kw...)

x = @lift date2num.($t)
r = dateseries!(ax, x, y; kw...)

# 横坐标变化时,更新
onany(x, t; update=true) do x, t
xlims = extrema(x)
xlims!(ax, xlims)

## update ticks and xticklabels
xticks = guess_date_ticks(t; fmt=date_format)
ax.xticks[] = xticks
onany(t; update=true) do t
set_date_ticks!(ax, t; date_format)
end
r
end

function dateseries!(ax, t::AbstractVector{<:TypeDate}, y;
date_format="yy/mm/dd", kw...)

set_date_ticks!(ax, t; date_format)
dateseries!(ax, date2num.(t), y; kw...)
end
21 changes: 15 additions & 6 deletions src/Layers/imagesc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ Heatmap with colorbar
imagesc(rand(10, 10))
```
"""
function imagesc!(f, x, y, z::Union{R,Observable{R}};
function imagesc!(ax::Axis, x, y, z::Union{R,Observable{R}};
colorrange=automatic, col_rev=false, colors=:viridis,
title="Plot", fact=nothing, kw...) where {R<:AbstractArray{<:Real,2}}

fact=nothing, kw...) where {R<:AbstractArray{<:Real,2}}
col_rev && (colors = reverse(colors))

ax = Axis(f[1, 1]; title)
if fact === nothing
plt = heatmap!(ax, x, y, z; colorrange, colormap=colors, kw...)
else
Expand All @@ -39,12 +38,22 @@ function imagesc!(f, x, y, z::Union{R,Observable{R}};
plt = heatmap!(ax, x[1:fact:end], y[1:fact:end], _z;
colorrange, colormap=colors, kw...)
end
Colorbar(f[1, 2], plt)
plt
end

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

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

# for 3d array
function imagesc!(fig, x, y, z::Union{R,Observable{R}};
function imagesc!(fig::Union{Figure,GridPosition},
x, y, z::Union{R,Observable{R}};
colorrange=automatic,
layout=nothing,
titles=nothing, colors=:viridis, gap=10, kw...) where {R<:AbstractArray{<:Real,3}}
Expand Down
1 change: 1 addition & 0 deletions src/MakieLayers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ include("Layers/Layers.jl")
include("events.jl")
include("colormap.jl")
include("theme.jl")
include("Shapefile.jl")

end # module MakieLayers
4 changes: 4 additions & 0 deletions src/Shapefile.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function read_sf end
function plot_poly! end

export read_sf, plot_poly!
2 changes: 2 additions & 0 deletions src/colormap.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
export ncl_colors, ncl_group, amwg256
export nan_color
export resample_colors


using Serialization: serialize, deserialize
using Colors
using ColorSchemes

nan_color = RGBA(1.0, 1.0, 1.0, 0.2)

project_path(f...) = normpath(joinpath(@__DIR__, "..", f...))
load(f) = deserialize(project_path(f))
Expand Down
6 changes: 3 additions & 3 deletions src/theme.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
my_theme!(;fontsize=14)
```
"""
function my_theme!(; fontsize=14)
function my_theme!(; fontsize=14, titlesize=20)
kw_axes = (xticklabelsize=fontsize, yticklabelsize=fontsize,
titlesize=fontsize + 4,
titlesize=titlesize,
# titlefontsize=40,
xlabelsize=fontsize, ylabelsize=fontsize,
xlabelfont=:bold, ylabelfont=:bold)
mytheme = Theme(fontsize=14, Axis=kw_axes)
mytheme = Theme(;fontsize, Axis=kw_axes)
set_theme!(mytheme)
end

0 comments on commit a9514b8

Please sign in to comment.