Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Theme fixes #541

Merged
merged 5 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions docs/scripts/basics/plot_makie.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
using CairoMakie, Makie
using Rasters, RasterDataSources, ArchGDAL
A = Raster(WorldClim{BioClim}, 5) # this is a 3D raster, so is not accepted.
B = A[:, :, 1] # this converts to a 2D raster which Makie accepts!

fig = Figure()
plot(fig[1, 1], B)
contour(fig[1, 2], B)
plot(fig[1, 1], A)
contour(fig[1, 2], A)
ax = Axis(fig[2, 1]; aspect = DataAspect())
contourf!(ax, B)
surface(fig[2, 2], B) # even a 3D plot works!
contourf!(ax, A)
surface(fig[2, 2], A) # even a 3D plot works!
fig

# ### 3-D rasters in Makie
Expand All @@ -37,59 +36,60 @@ Rasters.rplot(stack; Axis = (aspect = DataAspect(),),)
# The plots seem a little squished here. We provide a Makie theme which makes text a little smaller
# and has some other space-efficient attributes:

CairoMakie.set_theme!(Rasters.theme_rasters())
Makie.set_theme!(Rasters.theme_rasters())
Rasters.rplot(stack)


# reset theme
using Makie
Makie.set_theme!()

# ### Plotting with `Observable`s, animations

# `Rasters.rplot` should support Observable input out of the box, but the dimensions of that input
# must remain the same - i.e., the element names of a RasterStack must remain the same.

CairoMakie.set_theme!(Rasters.theme_rasters())
Makie.set_theme!(Rasters.theme_rasters())

# `stack` is the WorldClim climate data for January
stack_obs = Observable(stack)
fig = Rasters.rplot(stack_obs;
Colorbar = (; height= Relative(0.75), width=5,)) # `stack` is the WorldClim climate data for January
Colorbar=(; height=Relative(0.75), width=5)
)
record(fig, "rplot.mp4", 1:12; framerate = 3) do i
stack_obs[] = RasterStack(WorldClim{Climate}; month = i)
end

# ![](rplot.mp4)

using Makie
Makie.set_theme!() # reset theme

# ```@docs
# Rasters.rplot
# ```

# ## using more vanilla Makie
# ## Using vanilla Makie

using Rasters, RasterDataSources

#the data
# The data
layers = (:evenness, :range, :contrast, :correlation)
st = RasterStack(EarthEnv{HabitatHeterogeneity}, layers)
ausbounds = X(100 .. 160), Y(-50 .. -10) # Roughly cut out australia
aus = st[ausbounds...] |> trim
#the plot
#colorbar attributes
aus = st[ausbounds...] |> Rasters.trim

# The plot
# colorbar attributes
colormap = :batlow
flipaxis = false
tickalign=1
width=13
ticksize=13;
#figure
width = 13
ticksize = 13
# figure
with_theme(theme_ggplot2()) do
fig = Figure(resolution=(800, 600))
axs = [Axis(fig[i,j], xlabel = "lon", ylabel = "lat") for i in 1:2 for j in 1:2]
plt = [Makie.heatmap!(axs[i], aus[l]; colormap) for (i,l) in enumerate(layers)]
[axs[i].title = string(l) for (i,l) in enumerate(layers)]
plt = [Makie.heatmap!(axs[i], aus[l]; colormap) for (i, l) in enumerate(layers)]
for (i, l) in enumerate(layers) axs[i].title = string(l) end
hidexdecorations!.(axs[1:2]; grid=false, ticks=false)
hideydecorations!.(axs[[2,4]]; grid=false, ticks=false)
Colorbar(fig[1, 0], plt[1]; flipaxis, tickalign, width, ticksize)
Expand Down
45 changes: 45 additions & 0 deletions ext/RastersMakieExt/plotrecipes.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@

const HIDE_DEC = (; label=true, grid=false, minorgrid=false, minorticks=false)

function Rasters.style_rasters()
Makie.Attributes(
Axis=(
xtickalign=1.0,
ytickalign=1.0,
xticklabelrotation=-π/4,
xticklabelsize=14,
yticklabelsize=14,
aspect=DataAspect(),
),
Colorbar=(
ticklabelsize=11,
tickalign=1.0,
),
)
end

function Rasters.color_rasters()
return Makie.Attributes(
colormap = :batlow,
)
end

function lift_layer(r::Observable, inds...)
return lift(lift_layer, r, inds...)
end
Expand Down Expand Up @@ -286,6 +309,22 @@ function Makie.plottype(raster::AbstractRaster{<:Union{Missing,Real},3})
Makie.Volume
end
end

function Makie.convert_arguments(t::Makie.PointBased, A::AbstractRaster{<:Any,1})
return Makie.convert_arguments(t, _prepare_dimarray(A))
end
function Makie.convert_arguments(t::Makie.PointBased, A::AbstractRaster{<:Number,2})
return Makie.convert_arguments(t, _prepare_dimarray(A))
end
function Makie.convert_arguments(t::Makie.SurfaceLike, A::AbstractRaster{<:Any,2})
return Makie.convert_arguments(t, _prepare_dimarray(A))
end
function Makie.convert_arguments(t::Makie.DiscreteSurface, A::AbstractRaster{<:Any,2})
return Makie.convert_arguments(t, _prepare_dimarray(A))
end
function Makie.convert_arguments(t::Makie.VolumeLike, A::AbstractRaster{<:Any,3})
return Makie.convert_arguments(t, _prepare_dimarray(A))
end
# allow plotting 3d rasters with singleton third dimension (basically 2d rasters)
function Makie.convert_arguments(x::Makie.ConversionTrait, raster::AbstractRaster{<:Union{Real,Missing},3})
D = _series_dim(raster)
Expand All @@ -305,3 +344,9 @@ function _series_dim(A)
spatialdims = (X(), Y(), Z())
last((dims(A, spatialdims)..., otherdims(A, spatialdims)...))
end

function _prepare_dimarray(A)
map(A) do x
isequal(x, missingval(A)) || ismissing(x) ? NaN32 : Float32(x)
end |> DimArray
end
11 changes: 11 additions & 0 deletions src/plotrecipes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,17 @@ function rplot(args...)
@error("Please load `Makie.jl` and then call this function. If Makie is loaded, then you can't call `rplot` with no arguments!")
end

# define the theme

# this function is defined so that we can override style_rasters in RastersMakieExt
function style_rasters end
function color_rasters end

function theme_rasters()
return merge(style_rasters(), color_rasters())
end


##################################################################################
# Utils

Expand Down
Loading