Setting both color maps and opacity when plotting rasters and heatmaps with Makie

Hi all, I’m looking to make a plot of some rasters (or if all else fails heatmaps) of values using Makie where the base color is independent of the opacity. Trying to blend two bits of information here where there are two different arrays, one for the colorrange, and one for alpha values.

From looking at the gallery and docs, they all mostly show alpha being fed in as a single parameter. Is there a way to achieve the above?

If you don’t have a specific package in mind, check the viz recipe in GeoStats.jl. You can pass a vector of alpha to achieve the desired result.

See a small imagealpha() recipe I shared some time ago here on discourse: How to implement Value Suppressing Colormaps in Makie - #6 by aplavin
Maybe image() should just support alpha=matrix natively?..

Thanks all for the suggestion. I had thought of viz but I think it only supports Polygonal data? The rasters would be matrix information.

As for the recipe by @aplavin, that certainly looks like an option. But I was having some trouble getting it to run as it seems Makie.MakieCore.color_and_colormap!() is not a function found in the library anymore. Has there been a change to the package perhaps?

The links were helpful in pointing to the Colorfy.jl package though. It’s a very in elegant solution, but I’ve tried using a scatter plot with :rect markers and precomputed colors with Colorfy.jl to produce a plot. But if any of you have more insights on the above, would be keen to hear!

viz supports all types of geospatial domains, including grids.

Check

and read

if you would like to dive into this ecosystem.

Here’s a manual solution, it’s not too difficult but yeah maybe Makie could have more convenience for it. I guess the reason it doesn’t is because a varying colormap plus varying alpha is difficult to interpret.

mat = reshape(1:16, 4, 4)
alphas = reshape(repeat(range(0, 1, length = 8), 2), 4, 4)

mi, ma = extrema(mat)
cg = cgrad(:viridis)
# index into colormap with values scaled to [0, 1]
colors = getindex.(Ref(cg), (mat .- mi) ./ (ma - mi))
colors_alpha = Makie.to_color(tuple.(colors, alphas))

image(colors_alpha)

Yes, Makie had a large breaking change recently. I don’t know where this specific function went, but you might try Makie.color_and_colormap! (ie, remove MakieCore).

I don’t think that function was ever public, might as well have disappeared in a non breaking release

Thanks to suggestion by @juliohm, I’ve managed to get it to with the Rasters.jl rasters, and have some nice plots!

@jules @aplavin I did try that fix at first, but it didn’t seem to work so I suspect that function isn’t available unfortunately.