How to make Makie themes work with plots inside loops?

CosmoProf
Sun, 15 Feb 2026 22:30:53 +0000
Visualization

Hi again, another unusual question about getting Makie themes to work. I need plot elements to keep getting added to a big plot inside a loop, but then the Makie “with_themes” command doesn’t work anymore. For example, I need to do something like this:

using MakiePublication
using CairoMakie
global figMyFig = Makie.Figure()
global axMyFig = Makie.Axis(figMyFig[1, 1])
#
for i in 1:5
    Makie.scatter!(axMyFig, (2.7*i),(1.7*i))
end
#
with_theme(theme_aps()) do
    fig = figMyFig
    ax = axMyFig
    Makie.display(fig)
end

But this does NOT do the same thing as:

using MakiePublication
using CairoMakie
with_theme(theme_aps()) do
    figMyFig2 = Makie.Figure()
    axMyFig2 = Makie.Axis(figMyFig2[1, 1])
    for i in 1:5
        Makie.scatter!(axMyFig2, (2.7*i),(1.7*i))
    end
    Makie.display(figMyFig2)
end

The latter gets the with_theme command right, but the former fails to get it working. (I’ve tried with and without the global commands, and numerous variations; nothing works.) Any ideas what’s causing this weird theme fail? Thanks!

(Note that I usually also have the Plots package loaded, which is why I put the “Makie.” in front of everything. But it doesn’t work even without Plots loaded.)

3 posts - 2 participants