Plots.jl colormap transparency

Using the GR backend, I am trying to recreate the following plot style:

Screenshot 2023-10-11 at 22.25.32

Note that the lower end of the colormap is transparent. There are some ideas for colormap transparency that however make the whole colorpalette transparent up to some alpha in the plot.

Any ideas on how to implement this style?

I created a colorscheme with alpha decreasing from 1 to 0 (you may change how is varying alpha according to your use case).

using ColorSchemes, Colors
using Plots
plotlyjs();
function modify_alpha(cscheme::ColorScheme, alpha::Vector{T}, newname::String) where T<: AbstractFloat
    size(cscheme.colors, 1) == size(alpha, 1) ||
          error("Vector alpha must have the same size as colors")
    csalpha=ColorScheme([Colors.RGBA(c.r, c.g, c.b, a) for (c,a) in zip(cscheme.colors, alpha)], newname, "")
end
ice_alpha=modify_alpha(ColorSchemes.ice, collect(range(1, 0, 256)), "mycscheme")

n=7
x=y=collect(1:n)
h=rand(n,n)
Plots.heatmap(x, y, h; color=palette(ice_alpha, 256),  size=(400,400), grid=false)

# Plot the same data as a heatmap with the original colorscheme, `ice`:
Plots.heatmap(x, y, h; color=palette(:ice, 256),  size=(400,400), grid=false)
1 Like

This works like a charm! Thanks a ton (:

This is a quick and dirty example for completeness,

begin
ice_alpha=modify_alpha(ColorSchemes.ice, collect(range(1, 0, 256)), "mycscheme")
x = range(0, stop=2π, length=100)
y = range(0, stop=2π, length=100)
z = [sin(xi + yi) for xi in x, yi in y]
heatmap(x, y, z, color=cgrad(palette(ice_alpha, 256), rev=true), xlabel="X", ylabel="Y", title="Sine Heatmap", grid=true)
end

My backend is GR and not PlotlyJS, this seems to work regardless. Makes sense since colors should be backend-independent.

Note that if the original coloscheme goes from lighter to darker colors, like the cscheme=ColorSchemes.matter,
then the vector alpha must have its values in increasing order, i.e.
alpha =range(0, 1, size(cscheme.colors, 1)).

  • Would it be possible to have an MWE here?
  • It would be really nice if the output image/solution were included.
begin
   using ColorSchemes, Colors
   using Plots
   function modify_alpha(cscheme::ColorScheme, alpha::Vector{T}, newname::String) where T<: AbstractFloat
       size(cscheme.colors, 1) == size(alpha, 1) ||
             error("Vector alpha must have the same size as colors")
       csalpha=ColorScheme([Colors.RGBA(c.r, c.g, c.b, a) for (c,a) in zip(cscheme.colors, alpha)], newname, "")
   end
   ice_alpha=modify_alpha(ColorSchemes.ice, collect(range(1, 0, size(ColorSchemes.ice.colors, 1))), "newice")
   x = range(0, stop=2π, length=100)
   y = range(0, stop=2π, length=100)
   z = [sin(xi + yi) for xi in x, yi in y]
   p = heatmap(x, y, z, color=cgrad(palette(ice_alpha, 256), rev=true), xlabel="X", ylabel="Y", title="Sine Heatmap", grid=true)
   savefig(p, "mwe.png")
end

mwe