Customising the legend display order in Plots.jl

I have found that I can edit the order directly, but the solution is not very elegant.

using Plots
using LaTeXStrings
pgfplotsx()

x_actual = LinRange(0, 2*pi, 1000)
x_obs = LinRange(0, 2*pi, 10)

n_lines = 3
# Plot underlying relationship
plt = plot(x_actual, (A->sin.(x_actual).*sqrt(A)).(1:n_lines); 
    labels=[L"" L"" L""],
    markershape=:none, 
    linestyle=:dash
)
# Plot sampled points with markers
plot!(x_obs, (A->sin.(x_obs).*sqrt(A)).(1:n_lines); 
    labels=[L"A^2=1" L"A^2=2" L"A^2=3"],
    markershapes=:auto, 
    linealpha=0
)

# Manually edit the order of the series
n_rows = n_lines
n_cols = 2
plt.series_list = [plt.series_list[1 + (i ÷ n_cols % n_rows) + (i % n_cols) * n_rows] for i in 0:length(plt.series_list)-1]
# Edit attributes that are todo with the indices
for i in 1:length(plt.series_list)
    plt.series_list[i].plotattributes[:series_index] = i
    plt.series_list[i].plotattributes[:series_plotindex] = i
end
# Update the subplot series list, as it still points at the original object
plt.subplots[1].series_list = plt.series_list

plot!(; legend_column=2, legend=:topright, grid=nothing, legend_foreground_color=nothing)

The result is below:

1 Like