How to draw a graph with GraphMakie and remove whitespace around it?

There is an example in GraphMakie.jl

using CairoMakie
using GraphMakie
using Graphs

g = wheel_graph(10)
f, ax, p = graphplot(g)
hidedecorations!(ax); hidespines!(ax)
ax.aspect = DataAspect()

The result is a picture like this:

If you open this PNG in a image editor, you can see that there are actually a lot of white space around it. Is there a way to remove it?

I managed to reduce the whitespace with the following code:

using CairoMakie
using GraphMakie
using Graphs

g = wheel_graph(10)
f, ax, p = graphplot(g)
hidedecorations!(ax); hidespines!(ax)
colsize!(f.layout, 1, Aspect(1, 1.0))
resize_to_layout!(f)

I guess this solution relies on knowing the aspect ratio of the plotted graph. I don’t know if there’s a more general solution.

1 Like

Thanks! That works. Would please elaborate a bit how it works?
It is really confusing how the width of the final picture is decided.

My understanding (not an expert here!) is that the figure and the axis are distinct entities. In your example, the figure is initiated with a default size. We then adjust the aspect ratio, but this only affects the axis, not the figure, leaving quite a bit of whitespace.

I would have to re-read the tutorial on aspect ratios; I think provides a lot more detail on this.

1 Like

That’s correct. When you give Axis an aspect, it just always resizes itself given the currently available space, but that’s after the layout is computed. So for resize_to_layout! the final size of the Axis is not known, so it cannot remove the whitespace. But giving rows or columns a fixed aspect ratio does let resize_to_layout! determine how much excess space there is.

3 Likes