Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
makieplots.jl 1.95 KiB
### Persefone.jl - a model of agricultural landscapes and ecosystems in Europe.
###
### This file visualises model output using Makie.
###

"""
    visualisemap(model, date)

Draw the model's land cover map and plot all individuals as points on it at the
specified date. If no date is passed, use the last date for which data are available.
Returns a Makie figure object.
"""
function visualisemap(model::AgentBasedModel, date::Union{Date,Nothing}=nothing)
    # extract the data from the model and load the map
    inds = model.datatables["individuals"]
    isnothing(date) && (date = inds.Date[end])
    landcover = load(@param(world.landcovermap))
    # draw the map
    f = Figure()
    ax = Axis(f[1,1])
    hidedecorations!(ax)
    image!(f[1,1], rotr90(landcover))
    ax.aspect = DataAspect()
    # plot individuals
    for s in unique(inds.Species)
        points = @select!(@subset(inds, :Species .== s, :Date .== date), :X, :Y)
        # 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
        @transform!(points, :Y = size(model.landscape)[2] .- :Y)
        scatter!(f[1,1], Matrix{Float32}(points), markersize=10)
    end
    f
end

"""
    populationtrends(model)

Plot a line graph of population sizes of each species over time.
Returns a Makie figure object.
"""
function populationtrends(model::AgentBasedModel)
    pops = model.datatables["populations"]
    dates = @param(core.startdate):@param(core.enddate)
    f = Figure()
    ax = Axis(f[1,1], xlabel="Date", ylabel="Population size",
              xtickformat = timeseries -> [Dates.format(dates[Int(d+1)], "dd/mm/yyyy")
                                           for d in timeseries])
    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
    axislegend("Species"; position=:lt)
    f
end