GalerkinToolkit.jl icon indicating copy to clipboard operation
GalerkinToolkit.jl copied to clipboard

GLMakie with transparent background

Open Cmurilochem opened this issue 1 year ago • 2 comments
trafficstars

@fverdugo and @raar1. While investigating this possibility I came across the following issue in the official Makie.jl repo: https://github.com/MakieOrg/Makie.jl/issues/4007

It seems indeed that there is currently a bug in GLMakie which prevents one to make figures with transparent background.

I tested it with GalerkinToolkit example:

import GalerkinToolkit as GT
import PartitionedSolvers as PS
import ForwardDiff
import GLMakie
using LinearAlgebra

function ex()
    domain = (0,1,0,1)
    cells = (10,10)
    mesh = GT.cartesian_mesh(domain,cells;simplexify=true)
    dirichlet_tag = "dirichlet"
    GT.label_boundary_faces!(mesh;physical_name=dirichlet_tag)
    Ω = GT.interior(mesh)
    Γd = GT.boundary(mesh;physical_names=[dirichlet_tag])
    k = 1
    V = GT.lagrange_space(Ω,k;dirichlet_boundary=Γd)
    uhd = GT.dirichlet_field(Float64,V)
    g = GT.analytical_field(x->0,Ω)
    f = GT.analytical_field(x->1,Ω)
    GT.interpolate_dirichlet!(g,uhd)
    dΩ = GT.measure(Ω,2*k)
    ∇ = ForwardDiff.gradient
    a(u,v) = GT.∫( x->∇(u,x)⋅∇(v,x), dΩ)
    l(v) = GT.∫( x->v(x)*f(x), dΩ)
    p = GT.linear_problem(uhd,a,l)
    s = PS.LinearAlgebra_lu(p)
    s = PS.solve(s)
    uh = GT.solution_field(uhd,s)

    bgcolor = :black
    fig = Figure(; size = (1500, 1500), backgroundcolor=bgcolor)

    scene = LScene(
       fig[1, 1],
       show_axis=true,
       scenekw = (
           lights=[],
           backgroundcolor=bgcolor,
           clear=true,
           transparency=true,
           ),
    )

    GLMakie.plot!(scene,Ω;color=uh,strokecolor=:black)
    fig
end

ex()

test

But if I set bgcolor = :transparent, The final figure is just a blank white canvas as described in https://github.com/MakieOrg/Makie.jl/issues/4007

The only work around this I could find is to use CairoMakie, like so:

import GalerkinToolkit as GT
import PartitionedSolvers as PS
import ForwardDiff
import CairoMakie
using LinearAlgebra

function ex()
    domain = (0,1,0,1)
    cells = (10,10)
    mesh = GT.cartesian_mesh(domain,cells;simplexify=true)
    dirichlet_tag = "dirichlet"
    GT.label_boundary_faces!(mesh;physical_name=dirichlet_tag)
    Ω = GT.interior(mesh)
    Γd = GT.boundary(mesh;physical_names=[dirichlet_tag])
    k = 1
    V = GT.lagrange_space(Ω,k;dirichlet_boundary=Γd)
    uhd = GT.dirichlet_field(Float64,V)
    g = GT.analytical_field(x->0,Ω)
    f = GT.analytical_field(x->1,Ω)
    GT.interpolate_dirichlet!(g,uhd)
    dΩ = GT.measure(Ω,2*k)
    ∇ = ForwardDiff.gradient
    a(u,v) = GT.∫( x->∇(u,x)⋅∇(v,x), dΩ)
    l(v) = GT.∫( x->v(x)*f(x), dΩ)
    p = GT.linear_problem(uhd,a,l)
    s = PS.LinearAlgebra_lu(p)
    s = PS.solve(s)
    uh = GT.solution_field(uhd,s)

    bgcolor = :transparent
    fig = Figure(; size = (1500, 1500), backgroundcolor=bgcolor)

    scene = LScene(
       fig[1, 1],
       show_axis=true,
       scenekw = (
           lights=[],
           backgroundcolor=bgcolor,
           clear=true,
           transparency=true,
           ),
    )

    CairoMakie.plot!(scene,Ω;color=uh,strokecolor=:black)
    fig

end

ex()

test

I am not sure if this is an options for us ? If yes, I need to investigate how to include a LScene into our custom GalerkinToolkit Makie plots which for what I could experience so far is not so straightforwards as I initially thought.

Cmurilochem avatar Nov 01 '24 08:11 Cmurilochem