# How to add a manually created legend inside the axis?

**URL:** <https://discourse.julialang.org/t/how-to-add-a-manually-created-legend-inside-the-axis/119893>\
**Category:** Visualization\
**Tags:** question, makie\
**Created:** [September 26, 2024, 5:16am UTC](https://discourse.julialang.org/t/how-to-add-a-manually-created-legend-inside-the-axis/119893 "2024-09-26T05:16:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [September 26, 2024, 5:16am UTC](https://discourse.julialang.org/t/how-to-add-a-manually-created-legend-inside-the-axis/119893/1 "2024-09-26T05:16:32Z")

</div>

```julia
using CairoMakie

f = Figure()

Axis(f[1, 1])

elem_1 = [LineElement(color = :red, linestyle = nothing),
          MarkerElement(color = :blue, marker = 'x', markersize = 15,
          strokecolor = :black)]

elem_2 = [PolyElement(color = :red, strokecolor = :blue, strokewidth = 1),
          LineElement(color = :black, linestyle = :dash)]

elem_3 = LineElement(color = :green, linestyle = nothing,
        points = Point2f[(0, 0), (0, 1), (1, 0), (1, 1)])

elem_4 = MarkerElement(color = :blue, marker = 'π', markersize = 15,
        points = Point2f[(0.2, 0.2), (0.5, 0.8), (0.8, 0.2)])

elem_5 = PolyElement(color = :green, strokecolor = :black, strokewidth = 2,
        points = Point2f[(0, 0), (1, 0), (0, 1)])

Legend(f[1, 2],
    [elem_1, elem_2, elem_3, elem_4, elem_5],
    ["Line & Marker", "Poly & Line", "Line", "Marker", "Poly"],
    patchsize = (35, 35), rowgap = 10)

f

```

 ![5171eec.BwQjQ6iK.png (1200×900) (makie.org)](https://global.discourse-cdn.com/julialang/original/3X/5/5/55662b6746149981883a03e3b76e4d0a166ff124.png)

Is there any method to moving the legend entry patch rectangle to the inside of the axis? Like this:

 ![c52efe3.qkY0ndqV.png (1200×900) (makie.org)](https://global.discourse-cdn.com/julialang/original/3X/9/7/974d4a3bf70b82665af49cacb68e816eab6b9718.png)

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [September 26, 2024, 5:48am UTC](https://discourse.julialang.org/t/how-to-add-a-manually-created-legend-inside-the-axis/119893/2 "2024-09-26T05:48:34Z")

</div>

Just create it in the same cell. Change

> [@WuSiren](#):
>
> `Legend(f[1, 2],...`

To `Legend(f[1,1],...`  
But then you need to figure the how to position it properly and that I don’t know myself 🙂

Alternatively, you might be looking for the function `axislegend` which does that for you. However you already posted the image from its documentation so I assume you feel you need to use `Legend`?

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [September 26, 2024, 5:51am UTC](https://discourse.julialang.org/t/how-to-add-a-manually-created-legend-inside-the-axis/119893/3 "2024-09-26T05:51:32Z")

</div>

Thanks for your help.

It doesn’t matter which one I use, but the problem is that I seem to have to manually draw my own legend. Does `axislegend` support manually created legend?

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [September 26, 2024, 5:53am UTC](https://discourse.julialang.org/t/how-to-add-a-manually-created-legend-inside-the-axis/119893/4 "2024-09-26T05:53:51Z")

</div>

Oh… I got it!

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [September 26, 2024, 5:54am UTC](https://discourse.julialang.org/t/how-to-add-a-manually-created-legend-inside-the-axis/119893/5 "2024-09-26T05:54:10Z")

</div>

Thank you very much!

---

<div class="post-metadata">

**Author:** ![WuSiren](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wusiren/32/42529_2.png) [@WuSiren](https://discourse.julialang.org/u/WuSiren)\
**Post date:** [September 26, 2024, 5:56am UTC](https://discourse.julialang.org/t/how-to-add-a-manually-created-legend-inside-the-axis/119893/6 "2024-09-26T05:56:27Z")

</div>

```julia
f = Figure()

ax = Axis(f[1, 1])

elem_1 = [LineElement(color = :red, linestyle = nothing),
          MarkerElement(color = :blue, marker = 'x', markersize = 15,
          strokecolor = :black)]

elem_2 = [PolyElement(color = :red, strokecolor = :blue, strokewidth = 1),
          LineElement(color = :black, linestyle = :dash)]

elem_3 = LineElement(color = :green, linestyle = nothing,
        points = Point2f[(0, 0), (0, 1), (1, 0), (1, 1)])

elem_4 = MarkerElement(color = :blue, marker = 'π', markersize = 15,
        points = Point2f[(0.2, 0.2), (0.5, 0.8), (0.8, 0.2)])

elem_5 = PolyElement(color = :green, strokecolor = :black, strokewidth = 2,
        points = Point2f[(0, 0), (1, 0), (0, 1)])

axislegend(ax,
    [elem_1, elem_2, elem_3, elem_4, elem_5],
    ["Line & Marker", "Poly & Line", "Line", "Marker", "Poly"],
    patchsize = (35, 35), rowgap = 10)

f

```
