# Free legend placing

**URL:** <https://discourse.julialang.org/t/free-legend-placing/37910>\
**Category:** Visualization\
**Created:** [April 20, 2020, 4:49pm UTC](https://discourse.julialang.org/t/free-legend-placing/37910 "2020-04-20T16:49:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)\
**Post date:** [April 20, 2020, 4:49pm UTC](https://discourse.julialang.org/t/free-legend-placing/37910/1 "2020-04-20T16:49:23Z")

</div>

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:

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

```

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

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [April 20, 2020, 7:20pm UTC](https://discourse.julialang.org/t/free-legend-placing/37910/2 "2020-04-20T19:20:16Z")

</div>

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):

```julia
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

```

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

```

---

<div class="post-metadata">

**Author:** ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)\
**Post date:** [April 21, 2020, 11:28pm UTC](https://discourse.julialang.org/t/free-legend-placing/37910/3 "2020-04-21T23:28:35Z")

</div>

Hi @stillyslalom,

thanks! This is exactly what I was looking for!

jamble
