Band filled by vertical gradient in Makie.jl

Hello,

I would like to make a band filled by a vertical gradient using CairoMakie.jl. The result should be similar to this example.

How can I do it in a clear way using CairoMakie.jl?

I noticed that the following code

x = range(-2, 2, 100) |> collect
y = exp.(-x.^2)

fig = Figure()
ax = Axis(fig[1, 1], xlabel="x", ylabel="y")

band!(ax, x, zeros(length(x)), y, color=range(0, 1, 100))

fig

generates this figure

But I want the gradient in the y-axis.

The documentation says

Sets the color of the mesh. Can be a Vector{<:Colorant} for per vertex colors or a single Colorant . A Matrix{<:Colorant} can be used to color the mesh with a texture, which requires the mesh to contain texture coordinates.

But I don’t know how to use the matrix case.

The following code works

fig = Figure()
ax = Axis(fig[1, 1])

points = Point2f[(x_i, y_i) for (x_i, y_i) in zip(x, y)]

poly!(ax, points, color = y)

fig

It uses poly instead of band. However, the colormap is mapped following the edges and not the y axis. Indeed, if I use a colormap involving some transparency, I get something strange

x = range(-2, 2, 100) |> collect
y = exp.(-x.^2)

fig = Figure()
ax = Axis(fig[1, 1])

points = Point2f[(x_i, y_i) for (x_i, y_i) in zip(x, y)]

mycolor = colorant"purple"
cmap = range(RGBAf(mycolor.r, mycolor.g, mycolor.b, 0.0), RGBAf(mycolor.r, mycolor.g, mycolor.b, 1.0), 100)

poly!(ax, points, color = y, colormap = cmap)

fig

Looking at the recipe of the function density you could try something like this:

using CairoMakie
x = range(-2, 2, 100) |> collect
y = exp.(-x.^2)
f = Figure()
Axis(f[1, 1])
lines!(x, y, linewidth = 4, color = :black)
lowerv = Point2f.(x, zeros(length(x)))
upperv = Point2f.(x, y)
col = vcat(Float32[1-l[2] for l in lowerv], Float32[1-l[2] for l in upperv])
band!(lowerv, upperv, color=col, colormap = :grays)
f

1 Like

Thanks a lot!

With PNG everything already works. However, I have to put the rasterize=true as an argument of band! to correctly export it as a PDF.