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

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:

But I’m looking for a Makie specific solution.

Maybe this example from the docs: scatter ?

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

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.

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 and examples for colored lines:

   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

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

2 Likes

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