Skip to content

Instantly share code, notes, and snippets.

@NoFishLikeIan
Last active November 30, 2021 14:05
Show Gist options
  • Save NoFishLikeIan/aab7aaa2c6074e74346ca071d49c56b4 to your computer and use it in GitHub Desktop.
Save NoFishLikeIan/aab7aaa2c6074e74346ca071d49c56b4 to your computer and use it in GitHub Desktop.
Plot attractors and associated basins, as returned by `DynamicalSystems.basins_of_attraction`
function plotattractors(basins, attractors, x_space; kwargs...)
lims = extrema(x_space)
attractors_keys = keys(attractors) |> collect |> sort
isdivergent = -1 ∈ basins # Check whether there is a divergent region
isunique = length(unique(basins)) == 1 # Check whether the basin is uniform
levels = isdivergent ? [-1, attractors_keys..., Inf] : [attractors_keys..., Inf]
colorpalette = palette(:tab10)[1:length(attractors)]
colorswithdivergent = isdivergent ? [:gray, colorpalette...] : colorpalette # Add gray for divergent basin
colors = isunique ? [colorswithdivergent..., :black] : colorswithdivergent # Add dummy color if unique, contour doesn't like single colors
basinplot = contour(
x_space, x_space, basins';
xlabel = L"x_t", ylabel = L"y_t",
xlims = lims, ylims = lims,
fill = true, c = colors, alpha = 0.75,
levels = levels, legend = nothing, aspect_ratio=:equal, kwargs...)
for k ∈ attractors_keys
x, y = columns(attractors[k])
scatter!(basinplot, x, y; c = colorpalette[k], markerstrokecolor = :white, label = nothing)
end
return basinplot
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment