Plot legend with multiple columns

@kongdd, one workaround is to use insets, but example would definitely require more plumbing:

using Plots; gr()

# define n-curves and n-labels
n = 7
x = 0:0.1:6π
yk = [k*sin.(x .- k) .* exp.(-0.02*k*x) for k in 1:n]
labels = " k = " .* string.(1:n) .* " "

# plot n-curves and legend with n-labels over 2 columns
ncolors =  distinguishable_colors(n, [RGB(0,1,1), RGB(0,0,0)], dropseed=false)
n2 = ceil(Int,n/2)
b = 0.15  # adjust this function of legend sizes
p = plot(x,yk, xlabel="x", ylabel="y", color=ncolors', legend=false, widen=false)
plot!(p, (1:n2)', color=ncolors[1:n2]', inset=(1, bbox(1-2*b,0,b,1,:left)), bg=:transparent,
      subplot=2, legendfontsize=10, border=:none, label = permutedims(labels[1:n2]),
      bg_legend=:white, fg_legend=nothing, axes=false)
plot!(p, (n2+1:n)', color=ncolors[n2+1:n]', inset=(1, bbox(1-b,0, b,1,:left)), bg=:transparent,
      subplot=3, legendfontsize=10, border=:none, label = permutedims(labels[n2+1:n]),
      bg_legend=:white, fg_legend=nothing, axes=false)

Plot_gr_insets_for_legends_in_2_columns

1 Like