# Levels keyword but for scatter plot

**URL:** <https://discourse.julialang.org/t/levels-keyword-but-for-scatter-plot/70697>\
**Category:** Visualization\
**Tags:** question, makie\
**Created:** [October 31, 2021, 6:13pm UTC](https://discourse.julialang.org/t/levels-keyword-but-for-scatter-plot/70697 "2021-10-31T18:13:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [October 31, 2021, 6:13pm UTC](https://discourse.julialang.org/t/levels-keyword-but-for-scatter-plot/70697/1 "2021-10-31T18:13:55Z")

</div>

Hi!  
The `levels` keyword of `surface` is useful:

```julia
fig = Figure()
xs = LinRange(0, 20, 50)
ys = LinRange(0, 15, 50)
zs = [cos(x) * sin(y) for x in xs, y in ys]

ax, hm = contourf(fig[1, 1], xs, ys, zs,
    colormap = :Spectral, levels = [-1, -0.5, -0.25, 0, 0.25, 0.5, 1])
Colorbar(fig[1, 2], hm)
display(fig)

```

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

What I’d like to do is the same but now for a scatterplot where the marker color is the colorbar. Using `levels` with `scatter` errors:

```julia
fig = Figure()

xs = LinRange(0, 20, 50)

ys = LinRange(0, 15, 50)

zs = [cos(x) * sin(y) for (x,y) in zip(xs, ys)]

ax, hm = scatter(fig[1, 1], xs, ys; color = zs, colormap = :Spectral, levels = [-1, 0, 1])

Colorbar(fig[1, 2], hm)

display(fig)

```

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [October 31, 2021, 6:31pm UTC](https://discourse.julialang.org/t/levels-keyword-but-for-scatter-plot/70697/2 "2021-10-31T18:31:34Z")

</div>

Not sure about your use case, but here, without levels and using a categorical colormap. I hope it helps.

```julia
using CairoMakie, Colors, ColorSchemes
xs = LinRange(0, 20, 20)
ys = LinRange(0, 15, 20)
zs = [cos(x) * sin(y) for (x,y) in zip(xs, ys)]
fig = Figure()
ax, hm = scatter(fig[1, 1], xs, ys; color = zs, markersize = 20,
    colormap= cgrad(:Spectral_11, 3, categorical=true, rev=true),
    colorrange = (-1, 1))
cbar = Colorbar(fig[1, 2], hm, label="Levels")
cbar.ticks = ([-0.66, 0, 0.66], ["-1", "0", "1"])
fig

```

 ![plot_3](https://global.discourse-cdn.com/julialang/original/3X/9/9/9911ad690b6ba2b3686775430cf80680cb202177.png)

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [October 31, 2021, 6:33pm UTC](https://discourse.julialang.org/t/levels-keyword-but-for-scatter-plot/70697/3 "2021-10-31T18:33:27Z")

</div>

Yeah, this achieves what I want (besides the ticks not corresponding to real values). I wonder, can we automate it so that `scatter!` does this automatically given a `levels` keyword?

Notice: my use case is not a “categorical colormap”. I simply want to reduce the amount of possible colors from the 512 or so (default colormap) to e.g. 11. Reduce the amount of plotted information, but conserve a “continuous” colormap in a sense. Just what `levels` does for `surface` pretty much.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [October 31, 2021, 6:57pm UTC](https://discourse.julialang.org/t/levels-keyword-but-for-scatter-plot/70697/4 "2021-10-31T18:57:48Z")

</div>

> Reduce the amount of plotted information, but conserve a “continuous” colormap in a sense

That is what the `categorical` keyword does - it’s an ordinal color scale, not a nominal one.  
It would make sense to fix the color bar ticks though.
