Skip to content

Instantly share code, notes, and snippets.

@asinghvi17
Created February 28, 2023 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asinghvi17/90616428f628020387053568bb4b51f8 to your computer and use it in GitHub Desktop.
Save asinghvi17/90616428f628020387053568bb4b51f8 to your computer and use it in GitHub Desktop.
Basic faceting utility for Makie
# auto facet for makie
# Taken from Plots.jl
function compute_gridsize(numplts::Int; landscape = false, nr = -1, nc = -1)
# figure out how many rows/columns we need
if nr < 1
if nc < 1
nr = round(Int, sqrt(numplts))
nc = ceil(Int, numplts / nr)
else
nr = ceil(Int, numplts / nc)
end
else
nc = ceil(Int, numplts / nr)
end
if landscape
return min(nr, nc), max(nr, nc)
else
return max(nr, nc), min(nr, nc)
end
end
compute_gridsize((nr, nc); landscape = false) = (nr, nc)
get_all_contents(block::Makie.Block, contents = Makie.Block[]) = push!(contents, block)
function get_all_contents(layout::GridLayout, contents = Makie.Block[])
get_all_contents.(Makie.contents(layout), Ref(contents))
return contents
end
function facet(f, layout::GridLayout, data, nrows = -1, ncols = -1; landscape = false, hidex = true, hidey = true, linkaxes = true, extra_funs_on_axes = [])
nrows, ncols = compute_gridsize(length(data); landscape, nr = nrows, nc = ncols)
axs = []
for (i, datum) in enumerate(data)
grid_index = fldmod1(i, ncols)
f(layout[grid_index...], datum)
new_content = Makie.contents(layout[grid_index...])
ax_ind = findfirst(Makie.can_be_current_axis, new_content)
!isnothing(ax_ind) && push!(axs, new_content[ax_ind])
if hidex && grid_index[1] != nrows && !isnothing(ax_ind)
hidexdecorations!(new_content[ax_ind]; label = true, ticklabels = true, ticks = true, minorticks = true, grid = false, minorgrid = false)
end
if hidey && grid_index[2] != 1 && !isnothing(ax_ind)
hideydecorations!(new_content[ax_ind]; label = true, ticklabels = true, ticks = true, minorticks = true, grid = false, minorgrid = false)
end
end
if linkaxes && !(isempty(axs))
linkaxes!(axs...)
end
for fun in extra_funs_on_axes
fun(axs...)
end
return layout
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment