How do I make background in Makie transparent

I’m trying to get plots with transparent (nothing at all) background in Makie

using GLMakie
F = Figure()
ax = Axis(F[1,1])
θ = 0:0.1:2pi
X = cos.(θ)
Y = sin.(θ)
lines!(X,Y)
hidespines!(ax)
hidedecorations!(ax)

save("icon.png",F)

The saved file has a white background, is there anyway to make the background transparent (no background).

In Plots.jl one can do,

using Plots
θ = 0:0.1:2pi
X = cos.(θ)
Y = sin.(θ)
p=plot(X,Y, background_color=:transparent,
        legend = false,ticks=false, axis=false)
savefig(p, "test.png") 

test
really want to know how to do this in Makie :open_book::man_facepalming:

I am not certain this will work, but could you try:

ax = Axis(F[1,1], backgroundcolor=(:white,0))

Usually the element after the color is the alpha of the channel in other parts of Makie.

1 Like

Unfortunately it does not work😭.

The figure needs a transparent background, maybe Figure(backgroundcolor = :transparent) and Axis(backgroundcolor = :transparent) together?

:sob:sadly no.

I also tried to set both to (:white,0) , and other combinations of the two, they all failed. :disappointed_relieved:

Maybe that’s a problem with the background in GLMakie @sdanisch ? I think it should be no problem in CairoMakie to get a fully transparent background

I tried CairoMakie, and didn’t work, maybe I’m doing something wrong😥

using CairoMakie
# if, for example, GLMakie is activated already
CairoMakie.activate!()
F = Figure(backgroundcolor = :transparent)
ax = Axis(F[1,1], backgroundcolor = :transparent)
θ = 0:0.1:2pi
X = cos.(θ)
Y = sin.(θ)
lines!(X,Y)
hidespines!(ax)
hidedecorations!(ax)


save("1221344.png",F)

for me it worked

using CairoMakie
f = Figure(backgroundcolor = :transparent)

Axis(f[1, 1], backgroundcolor = :transparent)
lines!(1:10)

save("test.png", f)

2 Likes

YES!!! This works!!! (After I restarted the kernel in Jupiter Lab)

nice, I’ll check why it doesn’t work in glmakie

1 Like

Setting backgroundcolor in both Figure and Axis to (:white,0) has the same effect as :transparent in CairoMakie, they worked :blush: (failed in GLMakie :disappointed_relieved:)

F = Figure(backgroundcolor = (:white,0))

ax = Axis(F[1, 1], backgroundcolor =(:white,0))

For anyone finding this thread, there’s a workaround for GLMakie even if it cannot have transparent backgrounds natively

https://docs.makie.org/stable/how-to/save-figure-with-transparency/

2 Likes