Skip to content
Snippets Groups Projects
Commit 791ff1d8 authored by xo30xoqa's avatar xo30xoqa
Browse files

Refactored makieplots.jl

parent 629aaee2
No related branches found
No related tags found
No related merge requests found
name = "Persefone" name = "Persefone"
uuid = "039acd1d-2a07-4b33-b082-83a1ff0fd136" uuid = "039acd1d-2a07-4b33-b082-83a1ff0fd136"
authors = ["Daniel Vedder <daniel.vedder@idiv.de>"] authors = ["Daniel Vedder <daniel.vedder@idiv.de>"]
version = "0.3.5" version = "0.3.6"
[deps] [deps]
Agents = "46ada45e-f475-11e8-01d0-f70cc89e6671" Agents = "46ada45e-f475-11e8-01d0-f70cc89e6671"
......
...@@ -28,14 +28,16 @@ function visualisemap(model::AgentBasedModel,date=nothing,landcover=nothing) ...@@ -28,14 +28,16 @@ function visualisemap(model::AgentBasedModel,date=nothing,landcover=nothing)
return f return f
end end
isnothing(date) && (date = inds.Date[end]) isnothing(date) && (date = inds.Date[end])
update_theme!(palette=(color=cgrad(:seaborn_bright, length(unique(inds.Species))),), update_theme!(palette=(color=cgrad(:seaborn_bright,
length(@param(nature.targetspecies))),),
cycle=[:color]) cycle=[:color])
for s in unique(inds.Species) for s in @param(nature.targetspecies)
points = @select!(@subset(inds, :Species .== s, :Date .== date), points = @select!(@subset(inds, :Species .== s, :Date .== date),
:X, :Y) :X, :Y)
iszero(size(points)[1]) && continue
# The origin in Makie is in the bottom-left rather than in the top-left as # The origin in Makie is in the bottom-left rather than in the top-left as
# on the model map, so we have to invert the Y coordinates # on the model map, so we have to invert the Y coordinates
@transform!(points, :Y = size(model.landscape)[2] .- :Y) @transform!(points, :Y = size(landcover)[2] .- :Y)
scatter!(f[1,1], Matrix{Float32}(points), markersize=8) scatter!(f[1,1], Matrix{Float32}(points), markersize=8)
end end
f f
...@@ -50,7 +52,28 @@ Returns a Makie figure object. ...@@ -50,7 +52,28 @@ Returns a Makie figure object.
function populationtrends(model::AgentBasedModel) function populationtrends(model::AgentBasedModel)
pops = model.datatables["populations"] pops = model.datatables["populations"]
# create the labels needed for the ticks on the X axis # create the labels needed for the ticks on the X axis
update_theme!(palette=(color=cgrad(:seaborn_bright, length(unique(pops.Species))),),
cycle=[:color])
f = Figure()
dates = @param(core.startdate):@param(core.enddate) dates = @param(core.startdate):@param(core.enddate)
ax = Axis(f[1,1], xlabel="Date", ylabel="Population size",
limits=((1, length(dates)), nothing), xticks = gettickmarks(dates))
for s in @param(nature.targetspecies)
points = @select!(@subset(pops, :Species .== s), :Abundance)
iszero(size(points)[1]) && continue
lines!(f[1,1], Vector{Float32}(points.Abundance), linewidth=3, label=s)
end
size(pops)[1] > 0 && axislegend("Species"; position=:lt)
f
end
"""
gettickmarks(dates)
Given a vector of dates, construct a selection to use as tick mark locations.
Helper function for `[populationtrends](@ref)`
"""
function gettickmarks(dates)
ticks = (Int[], String[]) ticks = (Int[], String[])
for i in 1:length(dates) for i in 1:length(dates)
if Day(dates[i]) == Day(1) if Day(dates[i]) == Day(1)
...@@ -62,15 +85,7 @@ function populationtrends(model::AgentBasedModel) ...@@ -62,15 +85,7 @@ function populationtrends(model::AgentBasedModel)
deleteat!(ticks[1], 2:2:length(ticks[1])) deleteat!(ticks[1], 2:2:length(ticks[1]))
deleteat!(ticks[2], 2:2:length(ticks[2])) deleteat!(ticks[2], 2:2:length(ticks[2]))
end end
update_theme!(palette=(color=cgrad(:seaborn_bright, length(unique(pops.Species))),), ticks
cycle=[:color])
f = Figure()
ax = Axis(f[1,1], xlabel="Date", ylabel="Population size",
limits=((1, length(dates)), nothing), xticks = ticks)
for s in unique(pops.Species)
points = @select!(@subset(pops, :Species .== s), :Abundance)
lines!(f[1,1], Vector{Float32}(points.Abundance), linewidth=3, label=s)
end
size(pops)[1] > 0 && axislegend("Species"; position=:lt)
f
end end
#XXX add animation with record()? https://docs.makie.org/stable/explanations/animation/
...@@ -217,7 +217,7 @@ saving each figure as a PDF. ...@@ -217,7 +217,7 @@ saving each figure as a PDF.
""" """
function visualiseoutput(model::AgentBasedModel, format::String="pdf") function visualiseoutput(model::AgentBasedModel, format::String="pdf")
@debug "Visualising output." @debug "Visualising output."
CairoMakie.activate!() CairoMakie.activate!() # make sure we're using Cairo
for output in model.dataoutputs for output in model.dataoutputs
isnothing(output.plotfunction) && continue isnothing(output.plotfunction) && continue
figure = output.plotfunction(model) figure = output.plotfunction(model)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment