MakieOrg / Makie.jl

Interactive data visualizations and plotting in Julia
https://docs.makie.org/stable
MIT License
2.39k stars 304 forks source link

saving streamplot to svg is ~12x slower than saving to png #1986

Open lazarusA opened 2 years ago

lazarusA commented 2 years ago

Probably not much to do here, due to the high line resolution.

Also, I just noticed that the latex formatting is broken :(

using CairoMakie
x = y = -2:0.005:2
f(z) = 1 / (z * (z^2 - z - 1 - 3im))
fvals = [f(u + 1im * v) for u in x, v in y]
fvalues = abs.(fvals)
fargs = angle.(fvals)
polya(x, y) = Point2f(real(f(x + 1im * y)), -imag(f(x + 1im * y)))

fig = Figure(resolution = (900, 400))
axs = [Axis(fig[1, i], aspect = 1) for i in 1:2]
cmap = :roma
streamplot!(axs[1], polya, -2 .. 2, -2 .. 2, colormap = [:black, :red],
        gridsize = (40, 40), arrow_size = 6, linewidth = 1)
pltobj2 = heatmap!(axs[2], x, y, fargs, colorrange = (-π, π), colormap = cmap)
streamplot!(axs[2], polya, -2 .. 2, -2 .. 2, colormap = [:black, :black],
        gridsize = (40, 40), arrow_size = 6, linewidth = 1)
Colorbar(fig[1, 3], pltobj2, ticks = ([-π, -π / 2, 0, π / 2, π],
        [L"-\pi", L"-\pi/2", L"0", L"\pi/2", L"\pi"]))
limits!(axs[1], -2, 2, -2, 2)
limits!(axs[2], -2, 2, -2, 2)
colsize!(fig.layout, 1, Aspect(1, 1.0))
colsize!(fig.layout, 2, Aspect(1, 1.0))
@time save("streamplot.png", fig) # second call on save
0.494885 seconds (2.47 M allocations: 68.547 MiB, 20.79% gc time)
CairoScreen{Cairo.CairoSurfaceBase{UInt32}} with surface:
Cairo.CairoSurfaceBase{UInt32}(Ptr{Nothing} @0x00007fcdfae058f0, 900.0, 400.0)

and for svg

@time save("streamplot.svg", fig) # second call on save
5.856108 seconds (50.80 M allocations: 1.164 GiB, 1.10% gc time)
CairoScreen{Cairo.CairoSurfaceIOStream{UInt32}} with surface:
Cairo.CairoSurfaceIOStream{UInt32}(Ptr{Nothing} @0x00007fcdf8f4b060, 675.0, 300.0, IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=0, maxsize=Inf, ptr=1, mark=-1))
jkrumbiegel commented 2 years ago

Can you test this once against #master? I recently merged two sets of improvements for CairoMakie's speed, although they weren't so much for line plots. But it might have helped a bit

lazarusA commented 2 years ago

for png, on second call is slightly the same. However, for svg it doesn't work [several minutes now and doesn't finish saving]...

(tmp) pkg> st
      Status `~/Documents/tmp/Project.toml`
  [13f3f980] CairoMakie v0.8.3 `https://github.com/JuliaPlots/Makie.jl.git:CairoMakie#master`
  [ee78f7c6] Makie v0.17.3 `https://github.com/JuliaPlots/Makie.jl.git#master`
asinghvi17 commented 2 years ago

This could be caused by the way we draw multicolored lines in CairoMakie...it's somewhat inefficient but there's no good way to do nonlinear gradients for arbitrary lines. You could try rasterize=10 to the streamplot, which should remove the worst offender for the SVG.

Moelf commented 2 years ago

so here? https://github.com/JuliaPlots/Makie.jl/blob/3a17241a0d5bb852be573ed723c4977847834256/CairoMakie/src/primitives.jl#L154-L163

asinghvi17 commented 2 years ago

Yep, pretty much. If there's a better way of doing a color gradient then I'm all ears :D (since this really messes the draw time up).

Still better than matplotlib where you have to use patches...