How to specify colors from a colormap to use in a theme in makie?

Hello,

I’d like to be able to specify a colormap, say Dark2, to be used as the automatic colors in the lines plot type. Reading around it seems like I should be using the theming system to do it. Unfortunately I’ve not been able to get it to work. Below is my best attempt thus far but it only results in the following error when I try

ERROR: MethodError: Cannot `convert` an object of type Observable{Any} to an object of type Dict{Symbol, Observable}

Here is the MWE. Any advice on how to proceed would be appreciated. Note I’ve also tried with set_theme!() but have the same error when I do.

function makie_theme_test()

    with_theme(
        Theme(
            palette=(color = :Dark2_8)
            Lines = (cycle = [:color])
        )
    ) do

        n = 100
        x = LinRange(0, 1, n)
        f = x .^ 2
        g = sin.(2π .* x)

        fig = Figure()
        ax = Axis(fig[1, 1], xlabel="x", ylabel="y")
        lines!(x, f)
        lines!(x, g)
        display(fig)
    end

end

Some things to fix in your MWE:

1.- commas. Inside the parenthesis after palette and lines, so that you are passing a tuple. Also you need a comma between those two lines.

2.-The way of constructing a list of colors from a name is with the function cgrad (maybe it should be an example?). This seems to work:

julia> function makie_theme_test()

           with_theme(
               Theme(
                   palette=(color = cgrad(:Dark2_8,10),),
                   Lines = (cycle = [:color],)
               )
           ) do

               n = 100
               x = LinRange(0, 1, n)
               f = x .^ 2
               g = sin.(2π .* x)

               fig = Figure()
               ax = Axis(fig[1, 1], xlabel="x", ylabel="y")
               lines!(x, f)
               lines!(x, g)
               display(fig)
           end

       end

Man - thank you! :slight_smile:

The commas always get me with Makie.

What about cgrad? Where did you find that? I’m just wondering how I might’ve stumbled across it or where I should have been looking.

cgrad always seemed like an unnecessarily opaque name imho. I wonder if there’s room to rename it.

To be honest I had to look at some of the source files https://github.com/MakieOrg/Makie.jl/blob/adc9d9ae3523f746f9c32652c833a59bfc0167b2/docs/colormap_generation.jl#L57 because I always forget about it.

I opened this Issue:

You can also use Makie.categorical_colors(gradname, n_colors) here.

I always use Makie.to_colormap

I get the following when I try to do that.

`to_colormap(cm, categories)` is deprecated. Use `Makie.categorical_colors(cm, categories)` for categorical colors, and `resample_cmap(cmap, ncolors)` for continous resampling.

Ah I meant just to_colormap(name) but it only works if you’re using a real categorical colormap with a small number of entries. Not for picking a few colors from a continuous map with many entries.