GraphMakie.jl cuts off text

207 views Asked by At

I would like to visualize some graphs with labeled vertices using GraphMakie.jl. Sadly, makie doesn't seem to include the labels into its bounding box-calculation and the labels are therefore cut off. I would expect there to be some feature to add some padding to the Axis-object, but I can't find anything like this.

Minimal example:

using Graphs, GraphMakie
fig = Figure()
for i in 1:2
    ax = Axis(fig[1,i])
    g = wheel_graph(i+1)
    graphplot!(ax, g, nlabels=["label" for _ in 1:i+1])
    hidedecorations!(ax)
end
display(fig)

cut off graph plot

Things I tried that didn't work:

  • adding protrusions: ax.alignmode = Mixed(right = Makie.Protrusion(50))
  • refreshing limits: autolimits!(ax)
  • changing the layout gap: colgap!(fig.layout, 50)
  • manually overriding the size: ax.width = 400
1

There are 1 answers

0
Corylus On

The area inside the rectangle is determined by the plotted range of data. Therefore, to make the labels fit in the rectangle, you have to adjust the range in the "data domain", i.e., using xlims and ylims. To add a margin to these ranges, you have to first know these ranges and hence do the call to the graph layout-function manually. I've implemented a wrapper that does this and adds a margin-property to adjust the margins:

using Graphs, GraphMakie, NetworkLayout, CairoMakie

function graphplotWithMargin!(
        ax::Axis, 
        g::SimpleGraph; 
        margin = (0.1, 0.1, 0.1, 0.1),
        layout = Spring(),
        args...)
    local pos = layout(g)
    graphplot!(ax, g; layout=_->pos, args...)
 
    local x0, x1 = minimum(map(x->x[1],pos)), maximum(map(x->x[1],pos))
    CairoMakie.xlims!(ax, x0 - margin[4] * (x1 - x0), x1 + margin[2] * (x1 - x0))

    local y0, y1 = minimum(map(x->x[2],pos)), maximum(map(x->x[2],pos))
    CairoMakie.ylims!(ax, y0 - margin[1] * (y1 - y0), y1 + margin[3] * (y1 - y0))
end
    
fig = Figure()
for i in 1:2
    ax = Axis(fig[1,i])
    g = wheel_graph(i+1)
    graphplotWithMargin!(ax, g, 
        nlabels=["label" for _ in 1:i+1], 
        margin=(0.05, 0.15, 0.05, 0.05))
    hidedecorations!(ax)
end
display(fig)

two labelled graphs; now the labels are fully visible