# Legend in Makie

**URL:** <https://discourse.julialang.org/t/legend-in-makie/23770>\
**Category:** New to Julia\
**Created:** [May 2, 2019, 12:16pm UTC](https://discourse.julialang.org/t/legend-in-makie/23770 "2019-05-02T12:16:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ksamtsak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ksamtsak/32/10585_2.png) [@ksamtsak](https://discourse.julialang.org/u/ksamtsak)\
**Post date:** [May 2, 2019, 12:16pm UTC](https://discourse.julialang.org/t/legend-in-makie/23770/1 "2019-05-02T12:16:44Z")

</div>

Hi, I’m looking how to add a legend to a plot in Makie. While there’s an exported function `AbstractPlotting.legend`, there’s no documentation and apparently no mention of it in any example in the docs.

Example:

```julia
> p= lines(0:0.1:10,rand(101), label="1")
> scatter!(p,rand(0:10,10),rand(10),label="2")
> legend!(p) # this doesn't work

```

I also tried something more that doesn’t work, inspired by [https://github.com/JuliaPlots/Makie.jl/issues/61#issuecomment-437918488](https://github.com/JuliaPlots/Makie.jl/issues/61#issuecomment-437918488)

```julia
> lgd= Makie.legend(p, ["1","2"], camera=campixel!, raw=true)

```

---

<div class="post-metadata">

**Author:** ![non-Jedi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-jedi/32/3645_2.png) [@non-Jedi](https://discourse.julialang.org/u/non-Jedi)\
**Post date:** [May 2, 2019, 12:47pm UTC](https://discourse.julialang.org/t/legend-in-makie/23770/2 "2019-05-02T12:47:42Z")

</div>

Is your use-case not covered by [colorlegend](http://juliaplots.org/MakieGallery.jl/stable/colors.html#Color-legends-1)?

Edit: No, I suppose it doesn’t. What are you trying to do with the `label` kwarg? I don’t see it listed as an available attribute, and looking at the source code, that’s never a kwarg that’s looked for afaict.

Edit2: In any case, here’s how I got your example to work:

```julia
using Makie

p= lines(0:0.1:10,rand(101), label="1")
scatter!(p,rand(0:10,10),rand(10),label="2")
lgd = legend([p[2], p[end]], ["1", "2"]; camera=campixel!, raw=true)
vbox(p, lgd)

```

---

<div class="post-metadata">

**Author:** ![ksamtsak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ksamtsak/32/10585_2.png) [@ksamtsak](https://discourse.julialang.org/u/ksamtsak)\
**Post date:** [May 2, 2019, 2:26pm UTC](https://discourse.julialang.org/t/legend-in-makie/23770/3 "2019-05-02T14:26:05Z")

</div>

This works, thanks a lot! Maybe it’s worth adding such an example to the gallery? I’m touching Makie for the first time now, so I’m not too confident adding that myself.

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [May 2, 2019, 2:37pm UTC](https://discourse.julialang.org/t/legend-in-makie/23770/4 "2019-05-02T14:37:55Z")

</div>

> Maybe it’s worth adding such an example to the gallery?

[There is an example](https://simondanisch.github.io/ReferenceImages/gallery//labels/index.html), and you found it… You just ignored, that you need to pass the legend labels AND the plots to it 😉

Of course, bad error messages for wrong arguments are to blame here, but not really sure how another example would improve that situation.

---

<div class="post-metadata">

**Author:** ![ksamtsak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ksamtsak/32/10585_2.png) [@ksamtsak](https://discourse.julialang.org/u/ksamtsak)\
**Post date:** [May 2, 2019, 2:47pm UTC](https://discourse.julialang.org/t/legend-in-makie/23770/5 "2019-05-02T14:47:41Z")

</div>

Indeed you’re right and thanks for pointing this out. It’s not sure others will find it there though or in the github issue where I did. Maybe it would help if it were on Makie’s docs as well.

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [May 2, 2019, 2:48pm UTC](https://discourse.julialang.org/t/legend-in-makie/23770/6 "2019-05-02T14:48:49Z")

</div>

It kind of is, since the MakieGallery, where you can find this, is right on the first page of the docs.  
I admit, right now it’s not that easy to find, though. It should get a better title + I should put legend/ colorlegend in the tag search.

---

<div class="post-metadata">

**Author:** ![ksamtsak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ksamtsak/32/10585_2.png) [@ksamtsak](https://discourse.julialang.org/u/ksamtsak)\
**Post date:** [May 2, 2019, 2:50pm UTC](https://discourse.julialang.org/t/legend-in-makie/23770/7 "2019-05-02T14:50:19Z")

</div>

Ah, if the page search doesn’t look in the code, that explains it. Thanks in any case!
