Makie: RGB legend colors

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

The following worked in CairoMakie 0.7:

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);

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.

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

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

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.


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)

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

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 in the documentation has the same issue (legends are shown as gray boxes).

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