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

**URL:** https://discourse.julialang.org/t/how-to-specify-colors-from-a-colormap-to-use-in-a-theme-in-makie/95981
**Category:** Visualization
**Tags:** makie
**Created:** [March 13, 2023, 1:53am UTC](https://discourse.julialang.org/t/how-to-specify-colors-from-a-colormap-to-use-in-a-theme-in-makie/95981 "2023-03-13T01:53:25Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![TI36XPro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ti36xpro/32/33658_2.png) [@TI36XPro](https://discourse.julialang.org/u/TI36XPro)
#### Post date: [March 13, 2023, 1:53am UTC](https://discourse.julialang.org/t/how-to-specify-colors-from-a-colormap-to-use-in-a-theme-in-makie/95981/1 "2023-03-13T01:53:25Z")

</div>

Hello,

I’d like to be able to specify a colormap, say [Dark2](https://docs.makie.org/stable/documentation/colors/index.html#colors), to be used as the automatic colors in the `lines` plot type. Reading around it seems like I should be using the [theming](https://docs.makie.org/stable/documentation/theming/index.html#cycles) 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

```julia
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.

```julia
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

```

---

<div class="post-metadata">

### Author: ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)
#### Post date: [March 13, 2023, 2:18am UTC](https://discourse.julialang.org/t/how-to-specify-colors-from-a-colormap-to-use-in-a-theme-in-makie/95981/2 "2023-03-13T02:18:57Z")

</div>

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

```

---

<div class="post-metadata">

### Author: ![TI36XPro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ti36xpro/32/33658_2.png) [@TI36XPro](https://discourse.julialang.org/u/TI36XPro)
#### Post date: [March 13, 2023, 2:24am UTC](https://discourse.julialang.org/t/how-to-specify-colors-from-a-colormap-to-use-in-a-theme-in-makie/95981/3 "2023-03-13T02:24:50Z")

</div>

Man - thank you! 🙂

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.

---

<div class="post-metadata">

### Author: ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)
#### Post date: [March 13, 2023, 2:31am UTC](https://discourse.julialang.org/t/how-to-specify-colors-from-a-colormap-to-use-in-a-theme-in-makie/95981/4 "2023-03-13T02:31:10Z")

</div>

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

---

<div class="post-metadata">

### Author: ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)
#### Post date: [March 13, 2023, 2:46am UTC](https://discourse.julialang.org/t/how-to-specify-colors-from-a-colormap-to-use-in-a-theme-in-makie/95981/5 "2023-03-13T02:46:05Z")

</div>

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](https://github.com/MakieOrg/Makie.jl/blob/adc9d9ae3523f746f9c32652c833a59bfc0167b2/docs/colormap_generation.jl#L57) because I always forget about it.

I opened this Issue:

> <https://github.com/MakieOrg/Makie.jl/issues/2748>
>
> Related to this discourse topic: https://discourse.julialang.org/t/how-to-specif…y-colors-from-a-colormap-to-use-in-a-theme-in-makie/
> 
> It seems like the way of building a vector of colors from the name of a color scheme is difficult to find in the docs.
> 
> I am out of bandwidth currently so I open it in case someone can make a PR.

---

<div class="post-metadata">

### Author: ![asinghvi17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/asinghvi17/32/8272_2.png) [@asinghvi17](https://discourse.julialang.org/u/asinghvi17)
#### Post date: [March 13, 2023, 3:09am UTC](https://discourse.julialang.org/t/how-to-specify-colors-from-a-colormap-to-use-in-a-theme-in-makie/95981/6 "2023-03-13T03:09:51Z")

</div>

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

---

<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: [March 13, 2023, 6:36am UTC](https://discourse.julialang.org/t/how-to-specify-colors-from-a-colormap-to-use-in-a-theme-in-makie/95981/7 "2023-03-13T06:36:05Z")

</div>

I always use `Makie.to_colormap`…

---

<div class="post-metadata">

### Author: ![TI36XPro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ti36xpro/32/33658_2.png) [@TI36XPro](https://discourse.julialang.org/u/TI36XPro)
#### Post date: [March 13, 2023, 4:36pm UTC](https://discourse.julialang.org/t/how-to-specify-colors-from-a-colormap-to-use-in-a-theme-in-makie/95981/8 "2023-03-13T16:36:09Z")

</div>

I get the following when I try to do that.

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

```

---

<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: [March 13, 2023, 5:49pm UTC](https://discourse.julialang.org/t/how-to-specify-colors-from-a-colormap-to-use-in-a-theme-in-makie/95981/9 "2023-03-13T17:49:44Z")

</div>

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.
