Lots of lines! in Makie

I am making a plot of a dataset using semi-transparent lines connecting a bunch of measurements. MWE:

using CairoMakie, Colors
CairoMakie.activate!(type = "svg")

a = √.abs.(randn(500))
b = a .* 0.5 .+ randn() / 10
c = b .* 0.5 .+ randn() / 20

f = Figure()
ax = Axis(f[1,1]; xticks = (1:3, ["a", "b", "c"]))
ylims!(ax, (0.0, nothing))
for (a, b, c) in zip(a, b, c)
    lines!(ax, 1:3, [a, b, c]; color = colorant="0x1000")
end
f

which looks like

In the actual plot I have about 5000 calls to lines!. Seems to render fine in SVG too.

Is this idiomatic use of the Makie API, or can I somehow combine them into one call?

I think it’s idiomatic.

Alternatively, (I think) you can also concatenate all data into one vector, but insert a NaN as a separator between the data sets. The NaN takes care that the lines are not connected then.

But I am not sure how much this would help CairoMakie or GLMakie performance wise.
And if you want to later make it interactive then this only seems to be hindering you in updating the values, because you would have to take the NaNs into account again …

1 Like

For GLMakie, one linecall with nans will be much more performant!

2 Likes

The series recipe can be used to avoid manual NaN-business, which as Simon says is the most performant. Not sure if series actually does this under the hood, off the top of my head.

data = reduce(hcat, [(1:50) .+ i for i in 1:50])

series(data, solid_color = (:black, 0.1))

For CairoMakie, the difference in final output size should be relatively slight for SVG.

2 Likes