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

**URL:** <https://discourse.julialang.org/t/how-to-draw-a-graph-with-graphmakie-and-remove-whitespace-around-it/121008>\
**Category:** Graphs\
**Created:** [October 7, 2024, 1:27pm UTC](https://discourse.julialang.org/t/how-to-draw-a-graph-with-graphmakie-and-remove-whitespace-around-it/121008 "2024-10-07T13:27:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xing\_Shi\_Cai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xing_shi_cai/32/14128_2.png) [@Xing\_Shi\_Cai](https://discourse.julialang.org/u/Xing_Shi_Cai)\
**Post date:** [October 7, 2024, 1:27pm UTC](https://discourse.julialang.org/t/how-to-draw-a-graph-with-graphmakie-and-remove-whitespace-around-it/121008/1 "2024-10-07T13:27:08Z")

</div>

There is an [example in GraphMakie.jl](https://graph.makie.org/stable/generated/plots/)

```julia
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:

 ![7e8d3b49](https://global.discourse-cdn.com/julialang/original/3X/b/2/b2b85dcf8949eabcfeb7540613507b8130a26cf6.png)

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?

---

<div class="post-metadata">

**Author:** ![simonsteiger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonsteiger/32/209862_2.png) [@simonsteiger](https://discourse.julialang.org/u/simonsteiger)\
**Post date:** [October 7, 2024, 2:01pm UTC](https://discourse.julialang.org/t/how-to-draw-a-graph-with-graphmakie-and-remove-whitespace-around-it/121008/2 "2024-10-07T14:01:42Z")

</div>

I managed to reduce the whitespace with the following code:

```julia
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.

---

<div class="post-metadata">

**Author:** ![Xing\_Shi\_Cai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xing_shi_cai/32/14128_2.png) [@Xing\_Shi\_Cai](https://discourse.julialang.org/u/Xing_Shi_Cai)\
**Post date:** [October 7, 2024, 5:10pm UTC](https://discourse.julialang.org/t/how-to-draw-a-graph-with-graphmakie-and-remove-whitespace-around-it/121008/3 "2024-10-07T17:10:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![simonsteiger](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonsteiger/32/209862_2.png) [@simonsteiger](https://discourse.julialang.org/u/simonsteiger)\
**Post date:** [October 7, 2024, 7:15pm UTC](https://discourse.julialang.org/t/how-to-draw-a-graph-with-graphmakie-and-remove-whitespace-around-it/121008/4 "2024-10-07T19:15:42Z")

</div>

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](https://docs.makie.org/stable/tutorials/aspect-tutorial); I think provides a lot more detail on this.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [October 7, 2024, 8:02pm UTC](https://discourse.julialang.org/t/how-to-draw-a-graph-with-graphmakie-and-remove-whitespace-around-it/121008/5 "2024-10-07T20:02:49Z")

</div>

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.
