# Make a Legend with width equal to an axis

**URL:** <https://discourse.julialang.org/t/make-a-legend-with-width-equal-to-an-axis/97317>\
**Category:** Visualization\
**Tags:** question, makie\
**Created:** [April 10, 2023, 7:04pm UTC](https://discourse.julialang.org/t/make-a-legend-with-width-equal-to-an-axis/97317 "2023-04-10T19:04:26Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [April 10, 2023, 7:04pm UTC](https://discourse.julialang.org/t/make-a-legend-with-width-equal-to-an-axis/97317/1 "2023-04-10T19:04:26Z")

</div>

Hi, I am trying to recreate the following PyPlot image with Makie:

 ![analytic](https://global.discourse-cdn.com/julialang/original/3X/9/1/91032d473947044b0de192ad73998ffa5b9744d9.jpeg)

I know how to make everything in the figure except one thing: how to make the legend width to be _exactly_ the same as the axis below it. Any help would be appreciated. I have access to the `Axis` object.

I thought

```julia
Legend(fig[0, :], [l], ["test"]; tellheight = true, tellwidth = false, width = Auto())

```

would do the trick, but it doesn’t.

---

<div class="post-metadata">

**Author:** ![asinghvi17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asinghvi17/32/8272_2.png) [@asinghvi17](https://discourse.julialang.org/u/asinghvi17)\
**Post date:** [April 10, 2023, 8:48pm UTC](https://discourse.julialang.org/t/make-a-legend-with-width-equal-to-an-axis/97317/2 "2023-04-10T20:48:33Z")

</div>

Have you tried setting one of the Axes to tellwidth=true?

Also, what does the figure look like now?

---

<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:** [April 10, 2023, 9:07pm UTC](https://discourse.julialang.org/t/make-a-legend-with-width-equal-to-an-axis/97317/3 "2023-04-10T21:07:43Z")

</div>

The axes will automatically take the legend width if they’re in the same single column. You can use `resize_to_layout!` to remove the excess whitespace of the Figure.

```julia
f = Figure()

axs = map(1:2) do i
    ax = Axis(f[i+1, 1])
    lines!(ax, cumsum(randn(100)), label = "periodic")
    lines!(ax, cumsum(randn(100)), label = "quasiperiodic")
    lines!(ax, cumsum(randn(100)), label = "Koch snowflake")
    lines!(ax, cumsum(randn(100)), label = "Kaplan-Yorke map")
    lines!(ax, cumsum(randn(100)), label = "sphere")
    lines!(ax, cumsum(randn(100)), label = "SM (uniform)")
    return ax
end

Legend(f[1, 1], axs[1], nbanks = 3, tellheight = true)

resize_to_layout!(f)

f

```

 ![grafik](https://global.discourse-cdn.com/julialang/original/3X/1/3/13a9f820375fbb5ab860fb3f67b7bd5290e9072c.jpeg)

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [April 10, 2023, 9:08pm UTC](https://discourse.julialang.org/t/make-a-legend-with-width-equal-to-an-axis/97317/4 "2023-04-10T21:08:12Z")

</div>

Or maybe something like this:

```julia
julia> fig = Figure()
julia> ax = Axis(fig[2,1], tellwidth=true)
julia> ax2 = Axis(fig[3,1])
julia> [lines!(ax,rand(10),label=String(rand('a':'z',15))) for _ in 1:5]
julia> lg = Legend(fig[1,1],ax,"analytically shown Δ", tellheight=false, tellwidth=false, nbanks=3)
julia> lg.width[] = ax.width[]

```

 ![imagen](https://global.discourse-cdn.com/julialang/original/3X/e/1/e1303e2f0c1087b73563b1b3b84308d46a8a80b5.png)

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [April 10, 2023, 9:36pm UTC](https://discourse.julialang.org/t/make-a-legend-with-width-equal-to-an-axis/97317/5 "2023-04-10T21:36:09Z")

</div>

Thank you all for the advice. Oh, I wish I had a solution between that of @jules and @aramirezreyes …

The problem with @jules solution is that the figures are for publication; they need to have a specific size, otherwise it will messup the font size significantly when inserted into latex, and I really don’t want to deal with hte headache of the fontsize of the figure changing depending on how many letters my legend has…

@aramirezreyes solution is far superior here for the reason that the figure has a fixed size. Unfortunately, all legend entries are grouped in the middle, which appears weird.

By the way, since I know in advance the width of my figure, I could give all axes and the legend a fixed widht, like 700, but this doesn’t give results different from the @aramirezreyes solution:

```julia
f = Figure(resolution = (750, 500))
axs = map(1:2) do i
    ax = Axis(f[i+1, 1]; width = 700)
    lines!(ax, cumsum(randn(100)), label = "periodic")
    lines!(ax, cumsum(randn(100)), label = "quasiperiodic")
    lines!(ax, cumsum(randn(100)), label = "Koch snowflake")
    lines!(ax, cumsum(randn(100)), label = "Kaplan-Yorke map")
    lines!(ax, cumsum(randn(100)), label = "sphere")
    lines!(ax, cumsum(randn(100)), label = "SM (uniform)")
    return ax
end
Legend(f[1, 1], axs[1], nbanks = 3, tellheight = true, width = 700)
f

```

![image](https://global.discourse-cdn.com/julialang/original/3X/4/7/47a94ec756c740c54c45425b4d8d962d466e60d9.png)

having an evenly spaced out legend regardless of width seems like a nice feature to have, especially for publications. The situation I am trtying to achieve is common in papers I believe.

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [April 10, 2023, 9:45pm UTC](https://discourse.julialang.org/t/make-a-legend-with-width-equal-to-an-axis/97317/6 "2023-04-10T21:45:16Z")

</div>

No matter how advanced the tooling is, making plots always feels like artisanal labor.

```julia
f = Figure(resolution = (750, 500))
axs = map(1:2) do i
    ax = Axis(f[i+1, 1]; width = 700)
    lines!(ax, cumsum(randn(100)), label = "periodic")
    lines!(ax, cumsum(randn(100)), label = "quasiperiodic")
    lines!(ax, cumsum(randn(100)), label = "Koch snowflake")
    lines!(ax, cumsum(randn(100)), label = "Kaplan-Yorke map")
    lines!(ax, cumsum(randn(100)), label = "sphere")
    lines!(ax, cumsum(randn(100)), label = "SM (uniform)")
    return ax
end
 Legend(f[1, 1], axs[1],"analytically known Δ", nbanks = 3, tellheight = true, width = 700, patchsize=(40f0,20),colgap = 100)
f

```

 ![imagen](https://global.discourse-cdn.com/julialang/original/3X/a/e/aedb4f7ff1e56861c40887b6683977a84ada7c3b.png)

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [April 10, 2023, 10:04pm UTC](https://discourse.julialang.org/t/make-a-legend-with-width-equal-to-an-axis/97317/7 "2023-04-10T22:04:28Z")

</div>

> [@aramirezreyes](#):
>
> `patchsize=(40f0,20),colgap = 100`

Yeah but this seems to depend on the number of characters in my labels. I have 7 such figures in my paper so it becomes tedious to try and optimize these values for each one 😃

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [April 10, 2023, 10:05pm UTC](https://discourse.julialang.org/t/make-a-legend-with-width-equal-to-an-axis/97317/8 "2023-04-10T22:05:24Z")

</div>

But of course for now I’ll just live with having some decently large gap size and patch size and don’t worry too much that things aren’t perfectly aligned!

---

<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:** [April 11, 2023, 8:06am UTC](https://discourse.julialang.org/t/make-a-legend-with-width-equal-to-an-axis/97317/9 "2023-04-11T08:06:06Z")

</div>

You can measure the amount needed for the adjustment (using the rather internal but unlikely to change layout observables). You could also split the difference between column gaps and patch sizes depending on what you need:

```julia
f = Figure(resolution = (750, 500))

axs = map(1:2) do i
    ax = Axis(f[i+1, 1])
    lines!(ax, cumsum(randn(100)), label = "periodic")
    lines!(ax, cumsum(randn(100)), label = "quasiperiodic")
    lines!(ax, cumsum(randn(100)), label = "Koch snowflake")
    lines!(ax, cumsum(randn(100)), label = "Kaplan-Yorke map")
    lines!(ax, cumsum(randn(100)), label = "sphere")
    lines!(ax, cumsum(randn(100)), label = "SM (uniform)")
    return ax
end

function space_out!(legend)
    w_available = legend.layoutobservables.suggestedbbox[].widths[1]
    w_used = legend.layoutobservables.computedbbox[].widths[1]
    difference = w_available - w_used
    legend.colgap[] += difference / (legend.nbanks[] - 1)
    return
end

l1 = Legend(f[-1, 1], axs[1], "analytically known Δ", nbanks = 2, tellheight = true, tellwidth = false)
l2 = Legend(f[0, 1], axs[1], "analytically known Δ", nbanks = 3, tellheight = true, tellwidth = false)
l3 = Legend(f[1, 1], axs[1], "analytically known Δ", nbanks = 4, tellheight = true, tellwidth = false)

# update axis limits etc, the legend adjustment must come last
Makie.update_state_before_display!(f)

space_out!(l1)
space_out!(l2)
space_out!(l3)

f

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/4/24138f9ecea7479f6577d157268a990afe556c16.png)

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [April 11, 2023, 9:46am UTC](https://discourse.julialang.org/t/make-a-legend-with-width-equal-to-an-axis/97317/10 "2023-04-11T09:46:45Z")

</div>

Wow, thanks a lot; that is the solution!!!

---

<div class="post-metadata">

**Author:** ![asinghvi17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asinghvi17/32/8272_2.png) [@asinghvi17](https://discourse.julialang.org/u/asinghvi17)\
**Post date:** [April 13, 2023, 11:59am UTC](https://discourse.julialang.org/t/make-a-legend-with-width-equal-to-an-axis/97317/12 "2023-04-13T11:59:03Z")

</div>

This is a really cool solution. Should we have this as a kwarg to the Legend constructor (by default false)?

---

<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:** [April 13, 2023, 5:29pm UTC](https://discourse.julialang.org/t/make-a-legend-with-width-equal-to-an-axis/97317/13 "2023-04-13T17:29:26Z")

</div>

that wouldn’t work because you can only do this as the last step, and it depends on how exactly the legend is positioned whether it will work as intended
