# Makie Categorical colors

**URL:** <https://discourse.julialang.org/t/makie-categorical-colors/118967>\
**Category:** General Usage\
**Tags:** colors, makie\
**Created:** [September 2, 2024, 9:38pm UTC](https://discourse.julialang.org/t/makie-categorical-colors/118967 "2024-09-02T21:38:38Z")\
**Posts on this page:** 6\
**Page:** 1

<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:** [September 2, 2024, 9:38pm UTC](https://discourse.julialang.org/t/makie-categorical-colors/118967/1 "2024-09-02T21:38:38Z")

</div>

How to map a categorical column to a categorical color palette in Makie?

```julia
using CairoMakie,AlgebraOfGraphics,RDatasets
diamonds = dataset("ggplot2","diamonds")

```

In AoG it’s just

```julia
data(diamonds) * mapping(:Carat, :Price; color=:Color) * visual(Scatter) |> draw

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/8/88031293893c072ebd556bd7f4f0b6ce184d9383.png)

but how to do it in Makie?

```julia
julia> scatter(diamonds.Carat, diamonds.Price; color=string.(diamonds.Color))
ERROR: Unknown color: E

```

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [September 2, 2024, 9:51pm UTC](https://discourse.julialang.org/t/makie-categorical-colors/118967/2 "2024-09-02T21:51:44Z")

</div>

I thought “surely `Categorical` should work”, but surprisingly it doesn’t:

```julia
julia> scatter(rand(100), rand(100), color=Categorical(rand("abc", 100)))
ERROR: MethodError: no method matching to_color(::Categorical)

```

Probably a bug, as this

```julia
scatter(Categorical(rand("abc", 100)))

```

works.

---

<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:** [September 2, 2024, 10:05pm UTC](https://discourse.julialang.org/t/makie-categorical-colors/118967/3 "2024-09-02T22:05:38Z")

</div>

If I pass `color=` strings it tries to parse them as color names and passing `CategoricalArrays.CategoricalVector` fails, but passing numbers makes a plot.

That doesn’t seem like an ideal API in the sense that color names are very different from values to be mapped into colors so I wouldn’t expect them to be accepted by the same kwarg.

```julia
julia> scatter(
    diamonds.Carat, 
    diamonds.Price; 
    color=levelcode.(diamonds.Cut), 
    colormap=Makie.wong_colors(),
)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/8/787b541ac37716b2ec754499fb58c3d78427a0f1.jpeg)

Though is it me or is there too much yellow there, maybe it’s somehow treating it as continuous rather than categorical?

---

<div class="post-metadata">

**Author:** ![dpaetzel](https://avatars.discourse-cdn.com/v4/letter/d/ecd19e/32.png) [@dpaetzel](https://discourse.julialang.org/u/dpaetzel)\
**Post date:** [February 6, 2025, 4:26pm UTC](https://discourse.julialang.org/t/makie-categorical-colors/118967/4 "2025-02-06T16:26:37Z")

</div>

A workaround (or maybe the official solution?) is to set the `label` keyword to your category/string instead of the `color` keyword and use a loop.

EDIT: I misread the question/thread at first, sorry.

---

<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:** [February 9, 2025, 4:18pm UTC](https://discourse.julialang.org/t/makie-categorical-colors/118967/5 "2025-02-09T16:18:13Z")

</div>

A vector of colors passed to `colormap` is always interpreted as continuous. We probably need an overload to get `Categorical` inputs to color to just work…

---

<div class="post-metadata">

**Author:** ![zhanguozhou](https://avatars.discourse-cdn.com/v4/letter/z/bcef8e/32.png) [@zhanguozhou](https://discourse.julialang.org/u/zhanguozhou)\
**Post date:** [February 11, 2025, 7:03am UTC](https://discourse.julialang.org/t/makie-categorical-colors/118967/6 "2025-02-11T07:03:52Z")

</div>

The Makie has its own elegant methods only with the help of groupby() function from DataFrames, the following code works:

using CairoMakie, RDatasets, DataFrames  
diamonds = dataset(“ggplot2”,“diamonds”)  
fig = Figure()  
ax = Axis(fig[1,1])  
for i in groupby(diamonds, :color)  
scatter!(ax, i.carat, i.price, label=unique(i.color))  
end  
fig[1,2] = Legend(fig, ax, “Color”)
