Julia Plots.jl: changing marker colour from series recipe

I am trying to put together a Plots.jl user recipe, that would create several scatter plots and histograms arranged in a grid. The scatter plots should occupy the lower triangle of the grid. Histograms go on the diagonals. Each scatter plot should have several series, shown in different colours. The colours should be consistent across subplots.

My problem is when I allow the user to specify custom colours, the series colours in the scatter subplots get messed up.

Here is the (abridged) recipe code. data is is a vector of size r. Each element of data is a matrix of size n_r x d. n_r may vary, d stays the same. There should be d x (d-1) / 2 scatter plots, with r series on each plot, each series having n_r points.

@recipe function my_func(data::my_type; custom_colors=nothing)
    # get d, r, ...
    for i in i:d
        for j in 1:d
            @series begin
                subplot := (i - 1) * d + j
                if i == j
                    seriestype := :histogram
                    plot_data = # ... prepare data for histograms
                elseif j < i
                    # scatter subplot recipe
                    seriestype := :scatter
                    if custom_colors !== nothing
                        color := reshape(custom_colors, (1, r))
                    end
                    x = Vector()
                    y = Vector()
                    for r in runs
                        ser = data[r]
                        append!(x, [ser[:, j]])
                        append!(y, [ser[:, i]])
                    end # for r
                    plot_data = (x, y)
                else
                    # leave empty
                    plot_data = [0]
                end #  if/else
                plot_data  # return from the macro function
            end # @series
        end # for j
    end # for i
end # @recipe

My problem is that whenever I supply custom colours, I get inconsistency across subplots:

plot(my_data, custom_colors=["blue", "green", "black"])

plot with custom colors

Note how subplot (2, 1) has black dots in the middle, whereas all other scatter subplots have black on the outside

If I do the plot without custom colors:

plot(my_data)

I get consistent colours in all scatter subplots:

plot with default colors

Any clues why the colours are mixed up on the first plot?

1 Like

It’s typically not good form to just post a link to another site. Many of the active contributors here already monitor stack overflow, but if you want to use this forum to call attention to it it would be nice to include a bit of context rather than just a link.

Sorry, I thought the forum engine is intelligent enough to pull in the contents from SO, in the same manner as it has pulled in the header. Re-pasted the full question here as well. I wonder, what is the intention behind restricting the new posts to not more than one image?

Never mind, found the answer myself, and posted on SO