Skip to content

Commit

Permalink
Plot recipes: support automatic marker size with xlims and ylims (#…
Browse files Browse the repository at this point in the history
…677)

* add `xlims` and `ylmis`

* implement suggestions
  • Loading branch information
LasNikas authored Dec 2, 2024
1 parent a1cc9d6 commit c93ab39
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/visualization/recipes_plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ RecipesBase.@recipe function f(sol::TrixiParticlesODESolution)
end

RecipesBase.@recipe function f(v_ode, u_ode, semi::Semidiscretization;
size=(600, 400)) # Default size
size=(600, 400), # Default size
xlims=(-Inf, Inf), ylims=(-Inf, Inf))
systems_data = map(semi.systems) do system
u = wrap_u(u_ode, system, semi)
coordinates = active_coordinates(u, system)
Expand All @@ -25,14 +26,23 @@ RecipesBase.@recipe function f(v_ode, u_ode, semi::Semidiscretization;
x_min, y_min = minimum(coordinates, dims=2) .- 0.5particle_spacing
x_max, y_max = maximum(coordinates, dims=2) .+ 0.5particle_spacing

# `x_min`, `x_max`, etc. are used to automatically set the marker size.
# When `xlims` or `ylims` are passed explicitly, we have to update these to get the correct marker size.
isfinite(first(xlims)) && (x_min = xlims[1])
isfinite(last(xlims)) && (x_max = xlims[2])

isfinite(first(ylims)) && (y_min = ylims[1])
isfinite(last(ylims)) && (y_max = ylims[2])

return (; x, y, x_min, x_max, y_min, y_max, particle_spacing,
label=timer_name(system))
end

return (semi, systems_data...)
end

RecipesBase.@recipe function f((initial_conditions::InitialCondition)...)
RecipesBase.@recipe function f((initial_conditions::InitialCondition)...;
xlims=(Inf, Inf), ylims=(Inf, Inf))
idx = 0
ics = map(initial_conditions) do ic
x = collect(ic.coordinates[1, :])
Expand All @@ -46,6 +56,14 @@ RecipesBase.@recipe function f((initial_conditions::InitialCondition)...)
x_min, y_min = minimum(ic.coordinates, dims=2) .- 0.5particle_spacing
x_max, y_max = maximum(ic.coordinates, dims=2) .+ 0.5particle_spacing

# `x_min`, `x_max`, etc. are used to automatically set the marker size.
# When `xlims` or `ylims` are passed explicitly, we have to update these to get the correct marker size.
isfinite(first(xlims)) && (x_min = xlims[1])
isfinite(last(xlims)) && (x_max = xlims[2])

isfinite(first(ylims)) && (y_min = ylims[1])
isfinite(last(ylims)) && (y_max = ylims[2])

idx += 1

return (; x, y, x_min, x_max, y_min, y_max, particle_spacing,
Expand All @@ -56,12 +74,22 @@ RecipesBase.@recipe function f((initial_conditions::InitialCondition)...)
end

RecipesBase.@recipe function f(::Union{InitialCondition, Semidiscretization},
data...; zcolor=nothing, size=(600, 400), colorbar_title="")
data...; zcolor=nothing, size=(600, 400), colorbar_title="",
xlims=(Inf, Inf), ylims=(Inf, Inf))
x_min = minimum(obj.x_min for obj in data)
x_max = maximum(obj.x_max for obj in data)

y_min = minimum(obj.y_min for obj in data)
y_max = maximum(obj.y_max for obj in data)

# `x_min`, `x_max`, etc. are used to automatically set the marker size.
# When `xlims` or `ylims` are passed explicitly, we have to update these to get the correct marker size.
isfinite(first(xlims)) && (x_min = xlims[1])
isfinite(last(xlims)) && (x_max = xlims[2])

isfinite(first(ylims)) && (y_min = ylims[1])
isfinite(last(ylims)) && (y_max = ylims[2])

# Note that this assumes the plot area to be ~10% smaller than `size`,
# which is the case when showing a single plot with the legend inside.
# With the legend outside, this is no longer the case, so the `markersize` has to be
Expand Down

0 comments on commit c93ab39

Please sign in to comment.