Makie recipe and cycle colour

What is a good way to cycle through plot attributes in a recipe? In the example below, the first plot should use the same colours as second.

using Makie
using CairoMakie

# Recipe
@recipe TestPlot begin end

function Makie.plot!(p::TestPlot)
    for _ = 1:p.args[][1]
        lines!(p, rand(5))
    end
    return p
end

# Plot with and without recipe
fig = Figure()
Axis(fig[1, 1])
testplot!(10)
Axis(fig[2, 1])
for _ = 1:10
    lines!(rand(5))
end
fig

You have to declare which attributes to cycle in the theme via cycle = [:color] and similar, compare with Code search results · GitHub

Thanks a lot! However, adding cycle to the plot attributes does not have the desired effect.

using Makie
using CairoMakie

# Recipe
@recipe TestPlot begin
    cycle = [:color]
end

function Makie.plot!(p::TestPlot)
    for _ = 1:p.args[][1]
        lines!(p, rand(5))
    end
    return p
end

# Plot with and without recipe
fig = Figure()
Axis(fig[1, 1])
testplot!(10)
Axis(fig[2, 1])
for _ = 1:10
    lines!(rand(5))
end
fig

Ah I didn’t read your recipe right, you’re looping inside it. Cycling only works on the level of the plot object, not its nested parts. The cycler doesn’t see your inner lines, it just gives testplot a cycled color (if you declare it as an attribute).

In such a case it would be better to define a colormap attribute and then assign the colors using that. And it’s generally more performant if you merge your lines objects into a single NaN gapped one, especially if there are many lines.

You could also do:

function Makie.plot!(p::TestPlot)
    for i = 1:p.args[][1]
        lines!(p, rand(5), color=Cycled(i))
    end
    return p
end

oh really that works within recipes now?

Yes…and the recipe should actually also work!
I think its a problem in the cycle index here:

Great, that does the job for now, thanks a lot!

It actually worked like that some time ago.

can you open an issue with this as a regression, so we dont forget about it?

Would it actually cycle the “global” cycler? Ie, if I do testplot(...) and then lines!, would lines! use the next color in the cycler?

Of course, I just filed #5322.

1 Like