# Band filled by vertical gradient in Makie.jl

**URL:** <https://discourse.julialang.org/t/band-filled-by-vertical-gradient-in-makie-jl/124987>\
**Category:** Visualization\
**Tags:** makie, gradient, cairomakie\
**Created:** [January 20, 2025, 8:03pm UTC](https://discourse.julialang.org/t/band-filled-by-vertical-gradient-in-makie-jl/124987 "2025-01-20T20:03:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![albertomercurio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albertomercurio/32/27051_2.png) [@albertomercurio](https://discourse.julialang.org/u/albertomercurio)\
**Post date:** [January 20, 2025, 8:03pm UTC](https://discourse.julialang.org/t/band-filled-by-vertical-gradient-in-makie-jl/124987/1 "2025-01-20T20:03:02Z")

</div>

Hello,

I would like to make a band filled by a vertical gradient using CairoMakie.jl. The result should be similar to [this example](https://stackoverflow.com/questions/22081361/pyplot-vertical-gradient-fill-under-curve).

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

---

<div class="post-metadata">

**Author:** ![albertomercurio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albertomercurio/32/27051_2.png) [@albertomercurio](https://discourse.julialang.org/u/albertomercurio)\
**Post date:** [January 20, 2025, 9:25pm UTC](https://discourse.julialang.org/t/band-filled-by-vertical-gradient-in-makie-jl/124987/2 "2025-01-20T21:25:15Z")

</div>

I noticed that the following code

```julia
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

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/6/463af4986e047a5c48ed3a21786a16b23e6d2ab5.jpeg)

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.

---

<div class="post-metadata">

**Author:** ![albertomercurio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albertomercurio/32/27051_2.png) [@albertomercurio](https://discourse.julialang.org/u/albertomercurio)\
**Post date:** [January 20, 2025, 10:13pm UTC](https://discourse.julialang.org/t/band-filled-by-vertical-gradient-in-makie-jl/124987/3 "2025-01-20T22:13:08Z")

</div>

The following code works

```julia
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

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/7/770fbf45e798f4c35db344a0ce318643d5452cec.jpeg)

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

```julia
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

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/0/d0e0da6a54411f37d7f7a11cfcfa2735badbe726.jpeg)

---

<div class="post-metadata">

**Author:** ![Uve](https://avatars.discourse-cdn.com/v4/letter/u/82dd89/32.png) [@Uve](https://discourse.julialang.org/u/Uve)\
**Post date:** [January 20, 2025, 10:20pm UTC](https://discourse.julialang.org/t/band-filled-by-vertical-gradient-in-makie-jl/124987/4 "2025-01-20T22:20:29Z")

</div>

Looking at the recipe of the function [density](https://github.com/MakieOrg/Makie.jl/blob/c6e137d72c389dd7f20b57d59ded40fb1df48f06/src/stats/density.jl#L28) you could try something like this:

```julia
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

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/e/eee3cc677a1e534ceaddd61ecde8808705717f79.png)

---

<div class="post-metadata">

**Author:** ![albertomercurio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albertomercurio/32/27051_2.png) [@albertomercurio](https://discourse.julialang.org/u/albertomercurio)\
**Post date:** [January 20, 2025, 10:43pm UTC](https://discourse.julialang.org/t/band-filled-by-vertical-gradient-in-makie-jl/124987/5 "2025-01-20T22:43:55Z")

</div>

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.
