# Makie recipe for creating a scatter plot with points colored by value

**URL:** https://discourse.julialang.org/t/makie-recipe-for-creating-a-scatter-plot-with-points-colored-by-value/91754
**Category:** Visualization
**Created:** [December 16, 2022, 7:56pm UTC](https://discourse.julialang.org/t/makie-recipe-for-creating-a-scatter-plot-with-points-colored-by-value/91754 "2022-12-16T19:56:25Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![alex-s-gardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex-s-gardner/32/30210_2.png) [@alex-s-gardner](https://discourse.julialang.org/u/alex-s-gardner)
#### Post date: [December 16, 2022, 7:56pm UTC](https://discourse.julialang.org/t/makie-recipe-for-creating-a-scatter-plot-with-points-colored-by-value/91754/1 "2022-12-16T19:56:25Z")

</div>

Does anyone have a simple example of how to make a scatter plot using Makie.jl where the points are colored by value?

The same questions was posed more generally here:

> [@How to color points in scatter plot by value?](https://discourse.julialang.org/t/how-to-color-points-in-scatter-plot-by-value/11740):
>
> I have a list of 3-dimensional points. I want to plot them in a plane scatter plot (using the first two-dimensions), and then color each point according to the third value. How can I do this?

But I’m looking for a Makie specific solution.

---

<div class="post-metadata">

### Author: ![t-bltg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/t-bltg/32/25526_2.png) [@t-bltg](https://discourse.julialang.org/u/t-bltg)
#### Post date: [December 16, 2022, 8:00pm UTC](https://discourse.julialang.org/t/makie-recipe-for-creating-a-scatter-plot-with-points-colored-by-value/91754/2 "2022-12-16T20:00:02Z")

</div>

Maybe this example from the docs: [scatter](https://docs.makie.org/stable/examples/plotting_functions/scatter/#using_points) ?

---

<div class="post-metadata">

### Author: ![alex-s-gardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex-s-gardner/32/30210_2.png) [@alex-s-gardner](https://discourse.julialang.org/u/alex-s-gardner)
#### Post date: [December 16, 2022, 8:10pm UTC](https://discourse.julialang.org/t/makie-recipe-for-creating-a-scatter-plot-with-points-colored-by-value/91754/3 "2022-12-16T20:10:04Z")

</div>

OK I see that:

The color can also be set for each scattered marker by passing a `Vector` of colors or be used to index the `colormap` by passing a `Real` number or `Vector{<: Real}`.

I guess one just interpolates the colormap values by the data values to get an input for `color`

---

<div class="post-metadata">

### Author: ![t-bltg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/t-bltg/32/25526_2.png) [@t-bltg](https://discourse.julialang.org/u/t-bltg)
#### Post date: [December 16, 2022, 8:20pm UTC](https://discourse.julialang.org/t/makie-recipe-for-creating-a-scatter-plot-with-points-colored-by-value/91754/4 "2022-12-16T20:20:03Z")

</div>

> I guess one just interpolates the colormap values by the data values to get an input for `color`

If you are using `ColorSchemes` it’s pretty easy to sample the chosen colorscheme using [get](https://juliagraphics.github.io/ColorSchemes.jl/stable/basics/#Base.get).

---

<div class="post-metadata">

### Author: ![alex-s-gardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex-s-gardner/32/30210_2.png) [@alex-s-gardner](https://discourse.julialang.org/u/alex-s-gardner)
#### Post date: [December 19, 2022, 5:52pm UTC](https://discourse.julialang.org/t/makie-recipe-for-creating-a-scatter-plot-with-points-colored-by-value/91754/5 "2022-12-19T17:52:52Z")

</div>

This is much easier than I had thought, for anyone that comes next here is the recipe (See Makie docs has a more [generic scatter example](https://docs.makie.org/stable/examples/plotting_functions/scatter/#using_points) and examples for [colored lines](https://beautiful.makie.org/dev/examples/generated/2d/lines/line_cmaps/):

```julia
   using CairoMakie

    # create synthetic data
    n = 1000;
    x = 1:n;
    y = sin.(collect(x) .* .05);
    z = vcat(1:n/2, n/2:-1:1) .- n/4

    # set color axis limits which by default is automatically equal to the extrema of the color values
    colorrange = [-n/4, n/4];
    
    # choose color map [https://docs.juliahub.com/AbstractPlotting/6fydZ/0.12.10/generated/colors.html]
    cmap = :balance
    
    # make colored scatter plot
    fig = Figure()
    scatter(fig[1, 1], x, y; color=z, colormap = cmap, markersize=10, strokewidth=0, colorrange = colorrange)

    # add colorbar 
    Colorbar(fig[1, 2], colorrange = colorrange, colormap = cmap,
    label = "colored data")

    # show Figure
    fig

```

 ![example_scatter_colored](https://global.discourse-cdn.com/julialang/original/3X/a/3/a3b16a9c4f19274ce5b52fe33e26138ed7371741.jpeg)

---

<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: [December 19, 2022, 6:07pm UTC](https://discourse.julialang.org/t/makie-recipe-for-creating-a-scatter-plot-with-points-colored-by-value/91754/6 "2022-12-19T18:07:31Z")

</div>

Maybe it should be useful to take a look at the examples here:

> **[Lines with different Colorbars - Beautiful Makie](https://beautiful.makie.org/dev/examples/generated/2d/lines/line_cmaps/)**
>
> Makie Examples

---

<div class="post-metadata">

### Author: ![alex-s-gardner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex-s-gardner/32/30210_2.png) [@alex-s-gardner](https://discourse.julialang.org/u/alex-s-gardner)
#### Post date: [December 19, 2022, 6:15pm UTC](https://discourse.julialang.org/t/makie-recipe-for-creating-a-scatter-plot-with-points-colored-by-value/91754/7 "2022-12-19T18:15:04Z")

</div>

Very helpful… I hadn’t stumbled on that one yet. I’ve added the link to the solution.
