Free legend placing

Hi,
is it possible to place the legend using an exact position and not being restricted to :top, :bottom etc with Plots.jl and the pyplot backend? MWE:

plot([1,2,3],
  legend = (2,2)
)

Somethink similar for placing the legend at (2,2)? Thanks for your answers!

The syntax exists, but the legend location is specified as a fraction of the plotting domain, i.e. plot(legend=(0.5, 0.5)) would place it right in the middle. If you want to place it at a position determined by your data, try this (which uses internal attributes and will therefore be more fragile):

function legendloc!(p, xleg, yleg)
    sp_attrs = p.subplots[1].attr.explicit
    x = sp_attrs[:xaxis][:extrema]
    y = sp_attrs[:yaxis][:extrema]
    xloc = (xleg - x.emin)/(x.emax - x.emin)
    yloc = (yleg - y.emin)/(y.emax - y.emin)
    plot!(p, legend=(xloc, yloc))
end

p = plot(1:3)
legendloc!(p, 2, 2) # seems to work!
3 Likes

Hi @stillyslalom,

thanks! This is exactly what I was looking for!

jamble