# Making Plots place its legend in a better spot

Apparently this is a feature, or not of the backend, and Plots sends `legend=:best` to them. PyPlot should place the legend in a nice position by default, avoiding the data, but `GR` apparently does not have this feature. I think that is a sensible issue to open as a feature request (or just implement that, if anyone has any clue on how to do it).

1 Like

I’ve opened one issue in `GR.jl` about the legend position, which I think is the objective point here: https://github.com/jheinen/GR.jl/issues/444

If anyone has any idea on how to contribute to that, it would be nice.

This is a small function that can find the best legend position. I don’t know how to integrate it into the plots package, though (and probably it can be more general and simpler):

Code
``````using Plots

function find_best_legend_position(plt;nsamples=50)
ylims = Plots.ylims(plt)
xlims = Plots.xlims(plt)
dmin_max = 0.
ibest = 0
for series in plt.series_list
x = series[:x]
y = series[:y]
i = 0
for lim in Iterators.product(xlims,ylims)
i += 1
dmin = +Inf
for _ in 1:nsamples
isample = rand(1:length(x))
d = sum((lim .- (x[isample],y[isample])).^2)
if d < dmin
dmin = d
end
end
if dmin > dmin_max
dmin_max = dmin
ibest = i
end
end
end
ibest == 1 && return :bottomleft
ibest == 2 && return :bottomright
ibest == 3 && return :topleft
ibest == 4 && return :topright
end

``````

It could be used like this (not that I think that anynone will use it, but it may be a startup for doing that automatically):

``````x = 0:0.01:2;
plt = plot(x,x,label="linear")
plt = plot!(x,x.^3,label="cubic")
plt = plot!(legend=find_best_legend_position(plt)) # find best legend position
``````

will produce: or, for example,

``````x = 0:0.01:2;
plt = plot(x,-x,label="linear")
plt = plot!(x,-x.^3,label="cubic")
plt = plot!(legend=find_best_legend_position(plt)) # find best legend position
``````

will produce: 3 Likes

And if it is not possible to display the legend without hiding the data, it should be automatically placed outside the plot area.

That is relatively more complicated. I only check which point, between the extrema of the graph, has the maximum minimum distance to some randomly sampled points of the data. To check actual overlaps, one needs to take into account the legend size, and all the data, and that can be come cumbersome and expensive. Anyway the user can always (and probably will) change the legend position when fine tuning things.

1 Like

I would disagree with that. Changing the axes size is a lot more intrusive than just covering some data.

2 Likes

Despite the title of the thread, it may be interesting to let `Plot` do the job, and pass the best position to all backends (mentioned the possibility here).

Please feel free to change the title to better suit your objectives!

1 Like

Nothing is worse than hiding data. But there is another way, by plotting the legend inside the plot area, but extending the bounds to give it more space.

We are back on the subjective side of things, I prefer not to mess up with the plot size or figure size by using an option like this (otherwise much more plot attributes would become correlated). Anyway, as I mentioned, not overlapping with data is quite more complicated than the “reasonable position” option.

1 Like

Does mapplotlib picks an empty place in the plot like we saw in the example on the original thread, or it just happened that it uses an UpperLeft default position and by coincidence it didn’t overlap the example data? Finding the legend optimal position seems a tough problem and a potentially expensive one.

1 Like

I think it tries to pick an optimal place. The optimal position is subjective and potentially expensive. But guessing something “reasonable” most of the times is not (the function above takes 0.5s to run, and is not optimized in any sense (the time would increase by increasing the number of data series, but not the size of the data series themselves, because I choose a random sample from the data of constant size to check the overlaps - by decreasing the `nsamples` parameter the time decreases roughly proportionally).

edit: This version now takes `13 μs` to find the same good position for the legend (with the same number of samples):

``````
using Test
using Plots
using LinearAlgebra: norm

function dmin_series(lim,x,y,nsamples)
dmin = +Inf
for _ in 1:nsamples
isample = rand(1:length(x))
d = norm(lim .- (x[isample],y[isample]))
if d < dmin
dmin = d
end
end
return dmin
end

function find_best_legend_position(plt;nsamples=50)
ylims = Plots.ylims(plt)
xlims = Plots.xlims(plt)
dmin_max = 0.
ibest = 0
i = 0
for lim in Iterators.product(xlims,ylims)
i += 1
for series in plt.series_list
x = series[:x]
y = series[:y]
dmin = dmin_series(lim,x,y,nsamples)
if dmin > dmin_max
dmin_max = dmin
ibest = i
end
end
end
ibest == 1 && return :bottomleft
ibest == 2 && return :bottomright
ibest == 3 && return :topleft
return :topright
end

function test()

x = 0:0.01:2;
plt = plot(x,x,label="linear")
plt = plot!(x,x.^3,label="cubic")
@test find_best_legend_position(plt) == :topleft

x = 0:0.01:2;
plt = plot(x,-x,label="linear")
plt = plot!(x,-x.^3,label="cubic")
@test find_best_legend_position(plt) == :bottomleft

x = [0,1,0,1]
y = [0,0,1,1]
plt = scatter(x,y,xlims=[0.0,1.3],ylims=[0.0,1.3],label="test")
@test find_best_legend_position(plt) == :topright

plt = scatter(x,y,xlims=[-0.3,1.0],ylims=[-0.3,1.0],label="test")
@test find_best_legend_position(plt) == :bottomleft

plt = scatter(x,y,xlims=[0.0,1.3],ylims=[-0.3,1.0],label="test")
@test find_best_legend_position(plt) == :bottomright

plt = scatter(x,y,xlims=[-0.3,1.0],ylims=[0.0,1.3],label="test")
@test find_best_legend_position(plt) == :topleft

true
end

``````
1 Like

In an ideal world there would be an option to place the plot inside/outside the axes. Placing the legend outside ought to be upon the user’s request, both for simplicity of implementation and to match the typical user’s expectation.

Python’s `matplotlib` will happily place the legend on top of the data if there’s too much data. You can place the legend relative to the axis or relative to the figure (`ax` vs `plt` in common parlance), but it isn’t a walk in the park for the beginner.

1 Like
``````plot(rand(10),label="test",legend=:outertopright)
`````` 1 Like

I often find that if the legend has no frame, but a white (depending on the colorscheme) background at 50% opacity, the result is quite nice. You can still see the data behind the legend, and they don’t really clash.

This is only if there’s no place for the legend that doesn’t cover any data, of course.

4 Likes

Based on `@leandromartinez98`’s code, and I replaced Euclidean distance with Manhattan distance. Now, this version has the same outputs and about 2x acceleration.

``````using Plots

function datalims!(L, sx, sy, nsamples)
@inbounds for _ in 1:nsamples
i = rand(1:length(sx))
x, y = sx[i], sy[i]
L = min(L, x+y)
L = min(L, -x+y)
L = min(L, x-y)
L = min(L, -x-y)
end
end
function find_best_legend_position(plt; nsamples=50)
L = [Inf, Inf, Inf, Inf]
yb, yt = Plots.ylims(plt)
xl, xr = Plots.xlims(plt)
for series in plt.series_list
datalims!(L, series[:x], series[:y], nsamples)
end
@inbounds begin
L += -xl - yb
L += xr - yb
L += -xl + yt
L += xr + yt
end
i = argmax(L)
return (:bottomleft, :bottomright, :topleft, :topright)[i]
end
``````

I assumed the positive direction of the coordinate axis. I’m not sure if it’s correct.

3 Likes

A different approach using `findfirst()` which checks if some rectangular areas around the 4 corners are empty:

``````function placelegend()
p = Plots.current()
xl, yl = collect.(extrema.((xlims(p), ylims(p))))
dx, dy = (xl - xl)/3, (yl - yl)/4
x1, x2 = xl + [dx, -dx]
y1, y2 = yl + [dy, -dy]
tr = tl = br = bl = true
for series in p.series_list
x, y = series[:x], series[:y]
tr && (tr = isnothing(findfirst(@. (x > x2) & (y > y2))))
tl && (tl = isnothing(findfirst(@. (x < x1) & (y > y2))))
br && (br = isnothing(findfirst(@. (x > x2) & (y < y1))))
bl && (bl = isnothing(findfirst(@. (x < x1) & (y < y1))))
end
tr && return :topright
tl && return :topleft
br && return :bottomright
bl && return :bottomleft
return :outertopright  # did not find empty corner, place legend outside
end
``````
To test the code:
``````using Plots

# Ex.1:
x = 0:0.01:2;
plot(x,x,label="linear")
plot!(x,x.^3,label="cubic")
plot!(legend=placelegend(), fg_legend=:lightgrey)  # fg_legend=false

# Ex.2: repeat code block several times to test
x0 = rand(-1:0.1:1, 9); y0 = rand(-1:0.1:1, 9)
x1 = rand((-1,1)) * rand(1000)
y1 = rand((-1,1)) * rand(1000) .* x1
y2 = rand((-1,1)) * x1 .* y1.^2
y3 = rand((-1,1)) * x1 .* y1.^3
scatter(x0, y0, label="series0")
label=["series1" "series2" "series3"]
scatter!(x1, [y1, y2, y3], label=label);
scatter!(legend=placelegend(), fg_legend=:lightgrey)
`````` 3 Likes

I tried my code again with lmiq’s `test()` function under julia 1.7.2, and it still didn’t output any errors. Do you have some new test samples that failed? Can you share them?

I cannot reply the error anymore I will delete my comments

Ha ha, that’s life.