Makie color transparency


I want to plot points/lines with colors and changing transparencies. The Makie doc shows some usages, but what I need is to input a numerical value for color and another numerical value for the transparency.

This simple example works

using GLMakie
c = to_color(:purple)
npoints = 10
color = [RGBAf(c.r, c.g, c.b, (i/npoints)^2) for i in 1:npoints]
lines(1:10, 1:10; color)

so does this

lines(1:10, 1:10; color=1:10, colorrange=(1,10))

But what if I want something like

lines(1:10, 1:10; color=(1:10, 0.5), colorrange=(1,10)) # not working so far

i.e., changing color with a fixed transparency alpha? Or one step further, with a changing transparency setup?

For the first, do colormap = (colormap, alpha). For the second, you could construct the colormap with changing transparency. Like Colors.alphacolor.(Makie.to_colormap(x), alphavalues)

1 Like

Thank you! The first one works nicely, e.g.:

lines(1:10, 1:10; color=1:10, colorrange=(1,10), colormap=(:turbo, 0.5))

For the second one, although the following works:

using GLMakie.Colors
cmap = Colors.alphacolor.(resample_cmap(:turbo, 10), 0.1:0.1:1.0)
lines(1:10, 1:10; color=1:10, colorrange=(1,10), colormap=cmap)

It is still different from what I need. In this way for example, red always corresponds to high alpha, while purple always corresponds to low alpha. I want the transparency to be independent of RGB (i.e. the numerical value x). Logically adding alpha to the colormaps won’t work; probably what I need is a way to convert a numerical value x to RGB while keeping alpha a separate variable,

c = to_color(:purple)
npoints = 10
# Here RGB are fixed, but alpha is a variable; how to map a value x to the RGB in the target colormap?
color = [RGBAf(c.r, c.g, c.b, (i/npoints)^2) for i in 1:npoints]
julia> Makie.to_colormap(1.0)
ERROR: MethodError: no method matching to_colormap(::Float64)

julia> Makie.to_colormap([1.0, 2.0])
ERROR: MethodError: no method matching alpha(::Float32)

How should I do that?

We don’t have separate alpha (for other reasons) so you’d have to do the colormap lookup manually and then assign alpha values to those looked up colors. There is no clear public API for this though and we’re in the midst of refactoring colormaps and color conversions ourselves currently Clean up colormap handling and implement colorscale by SimonDanisch · Pull Request #2900 · MakieOrg/Makie.jl · GitHub

I’d like to do essentially the same thing. I’d like to set the color using the color map (based on a variable that varies between 0 and 1, but I’d like to vary the opacity so that early points are transparent and late points are opaque.
[imagine the first makie example here:

but the “trail” of the trajectory slowly fades. Note that this example seems to fade, but that is just an artifact of the colormap. If you use a different colormap (that doesn’t end in black), it won’t appear that way.]

Tyler.jl/docs/examples/UserGuide/whale_ex.jl at 9d32abe9f25a77d774f6b5d49f2f24667ef0e496 · MakieOrg/Tyler.jl · GitHub check here.

Thanks for the pointer. I still can’t figure out how to modify the Lorenz example to use a color map AND opacity at the same time. I don’t know how to modify the Observable for colors.