# Makie: RGB legend colors

**URL:** <https://discourse.julialang.org/t/makie-rgb-legend-colors/83086>\
**Category:** General Usage\
**Tags:** makie\
**Created:** [June 20, 2022, 5:58pm UTC](https://discourse.julialang.org/t/makie-rgb-legend-colors/83086 "2022-06-20T17:58:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [June 20, 2022, 5:58pm UTC](https://discourse.julialang.org/t/makie-rgb-legend-colors/83086/1 "2022-06-20T17:58:54Z")

</div>

How do I create legend entries in Makie using RBG colors?

The following worked in CairoMakie 0.7:

```julia
using CairoMakie, ColorSchemes

f = Figure()
Axis(f[1, 1])
labelV = ["lbl $j" for j = 1 : 3];
n = length(labelV);
elements = [PolyElement(color = ColorSchemes.corkO[i]) for i in 1:n];
Legend(f[1,2], elements, labelV);
f

```

In CairoMakie 0.8, it appears that the RBG colors returned by the color scheme are ignored. Replacing with `color = :red` works.

For background: I am creating a legend for a grouped bar graph. The legend colors are meant to match the bar colors. If there is a more direct way of accomplishing this, than manually pulling colors from the color theme, that would also be helpful.

---

<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:** [June 20, 2022, 6:32pm UTC](https://discourse.julialang.org/t/makie-rgb-legend-colors/83086/2 "2022-06-20T18:32:22Z")

</div>

Huh that’s weird, can you try what happens if you wrap the color with `RGBAf()`?

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [June 21, 2022, 12:02am UTC](https://discourse.julialang.org/t/makie-rgb-legend-colors/83086/3 "2022-06-21T00:02:49Z")

</div>

No change. All legend entries show grey colors. To be clear, what I’m doing now replaces one line from my original example with

```julia
elements = [PolyElement(color = RGBAf(ColorSchemes.corkO[i])) for i in 1:n];

```

However, with another color scheme (.e.g. `leonardo`) the colors are shown correctly. So I’m marking this as solved.

Thanks.

---

<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:** [June 21, 2022, 3:28am UTC](https://discourse.julialang.org/t/makie-rgb-legend-colors/83086/4 "2022-06-21T03:28:53Z")

</div>

Aha but cork is a colorscheme with lots of color entries, so just picking the first 3 or so will not give you different colors. You’d need to sample the colormap at `range(0, 1, length = n)`

---

<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:** [June 21, 2022, 3:30am UTC](https://discourse.julialang.org/t/makie-rgb-legend-colors/83086/5 "2022-06-21T03:30:12Z")

</div>

If you look in this list you can see that for cork the categorical display looks continuous, so it just has a lot of colors stored in small increments [Colors](https://makie.juliaplots.org/stable/documentation/colors/)

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [June 21, 2022, 12:16pm UTC](https://discourse.julialang.org/t/makie-rgb-legend-colors/83086/6 "2022-06-21T12:16:39Z")

</div>

Thanks - I should have put that into my MWE. In my actual code, this is taken care of properly.

It turns out that the actual issue is the plot theme. I ran my MWE in the environment of my plotting package, which had called `set_theme!(theme_ggplot2())`.

When I run in a “clean” environment or with another theme, it works all fine. Reviewing, I noticed that the [example bar graph](https://makie.juliaplots.org/v0.17.7/documentation/theming/predefined_themes/#theme_ggplot2) in the documentation has the same issue (legends are shown as gray boxes).

I should probably file an issue. Thanks again for your help.
