Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GLMakie with transparent background #133

Open
Cmurilochem opened this issue Nov 1, 2024 · 2 comments
Open

GLMakie with transparent background #133

Cmurilochem opened this issue Nov 1, 2024 · 2 comments
Labels

Comments

@Cmurilochem
Copy link
Collaborator

@fverdugo and @raar1. While investigating this possibility I came across the following issue in the official Makie.jl repo: MakieOrg/Makie.jl#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 MakieOrg/Makie.jl#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.

@fverdugo
Copy link
Collaborator

fverdugo commented Nov 4, 2024

Hi @Cmurilochem Thanks for the update.

CairoMakie can be a solution.

Does CairoMakie also work for 3d plots? and for creating animations?

@Cmurilochem
Copy link
Collaborator Author

Hi @Cmurilochem Thanks for the update.

CairoMakie can be a solution.

Does CairoMakie also work for 3d plots? and for creating animations?

Hi @fverdugo. It does make 2D and 3D plots, although it looks like the quality of these latter is slightly lower than GLMakie. However, as CairoMakie is expected to generate png-like figures, they are not iterative. The good thing is that we can use both packages at our convenience.

I will show you latter today how the documentation would look like with transparent plots done with CairoMakie.

See you soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants