# Legends on figure

**URL:** <https://discourse.julialang.org/t/legends-on-figure/39531>\
**Category:** New to Julia\
**Created:** [May 15, 2020, 4:01pm UTC](https://discourse.julialang.org/t/legends-on-figure/39531 "2020-05-15T16:01:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![O\_Julia](https://avatars.discourse-cdn.com/v4/letter/o/3ab097/32.png) [@O\_Julia](https://discourse.julialang.org/u/O_Julia)\
**Post date:** [May 15, 2020, 4:01pm UTC](https://discourse.julialang.org/t/legends-on-figure/39531/1 "2020-05-15T16:01:55Z")

</div>

I am using Plots.jl with plotly() backend. Legends of following figure are outer of the figure.  
 ![plotly_ex1](https://global.discourse-cdn.com/julialang/original/3X/a/0/a0e69ea47c27122847ae4f7ea9281501ea295f83.png)

Is it possible to take these legends on the figure as below

![gr_ex1](https://global.discourse-cdn.com/julialang/original/3X/f/c/fc6d886b33461fea21c954db1567511af776b036.png)

Thank you.

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [May 15, 2020, 5:13pm UTC](https://discourse.julialang.org/t/legends-on-figure/39531/2 "2020-05-15T17:13:47Z")

</div>

It is not easy with Plotly as far as I know. Some of the other backends use this legend behavior by default and support alternative legend placement via keyword arguments like `legend=:topright` and `legend=:outerbottomleft`. If you are willing to change backends, I’ve found that PyPlot does what I want automatically more often than the others.

---

<div class="post-metadata">

**Author:** ![josephmarturano](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@josephmarturano](https://discourse.julialang.org/u/josephmarturano)\
**Post date:** [May 15, 2020, 6:27pm UTC](https://discourse.julialang.org/t/legends-on-figure/39531/3 "2020-05-15T18:27:09Z")

</div>

You can pass a tuple of x-y legend positions and plotly will draw the legend at that location. For example:

```julia
using Plots
plotlyjs()
legendplot = plot(rand(10, 2), legend=(0.8, 0.9))
savefig(legendplot, "legendposition.png")
savefig(legendplot, "legendposition.html")

```

Producing the following:

![legendposition](https://global.discourse-cdn.com/julialang/original/3X/0/3/031f7a412d33c21db99aace35557484fbcc1d30d.png)

---

<div class="post-metadata">

**Author:** ![O\_Julia](https://avatars.discourse-cdn.com/v4/letter/o/3ab097/32.png) [@O\_Julia](https://discourse.julialang.org/u/O_Julia)\
**Post date:** [May 15, 2020, 6:49pm UTC](https://discourse.julialang.org/t/legends-on-figure/39531/4 "2020-05-15T18:49:16Z")

</div>

Thank you for reply, unfortunately I can not use pyplot() backend because of mkl.

---

<div class="post-metadata">

**Author:** ![O\_Julia](https://avatars.discourse-cdn.com/v4/letter/o/3ab097/32.png) [@O\_Julia](https://discourse.julialang.org/u/O_Julia)\
**Post date:** [May 15, 2020, 6:56pm UTC](https://discourse.julialang.org/t/legends-on-figure/39531/5 "2020-05-15T18:56:25Z")

</div>

Thank you, but you used plotlyjs(). When I use `legend=(0.8, 0.9)` with plotly(), nothing happens. If I use plotlyjs can I save it as .eps later?

---

<div class="post-metadata">

**Author:** ![josephmarturano](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@josephmarturano](https://discourse.julialang.org/u/josephmarturano)\
**Post date:** [May 15, 2020, 7:06pm UTC](https://discourse.julialang.org/t/legends-on-figure/39531/6 "2020-05-15T19:06:45Z")

</div>

Hmm, I see the legend inside the plot regardless if I use `plotly()` or `plotlyjs()`. Not sure.

Regardless, according to the Plots webpage ([Output · Plots](http://docs.juliaplots.org/latest/output/)), the `plotlyjs()` backend should enable saving as .eps.

---

<div class="post-metadata">

**Author:** ![O\_Julia](https://avatars.discourse-cdn.com/v4/letter/o/3ab097/32.png) [@O\_Julia](https://discourse.julialang.org/u/O_Julia)\
**Post date:** [May 15, 2020, 7:44pm UTC](https://discourse.julialang.org/t/legends-on-figure/39531/7 "2020-05-15T19:44:42Z")

</div>

```julia
using Plots

plotly()
x = collect(range(-64,64,length=400));
ua=(sech.(x))
u=(sech.(x))*2
p1=plot(x,[ua u],xlabel = "x", ylabel = "u", color = [:deepskyblue :red], label = ["Exact" "Numerical"], lw = 2,linealpha=[1.0 .2],marker = ( [:none :o],0.75, Plots.stroke(:black)),markersize=2, legend=(-35.0, 1.0),framestyle = :box, grid=false)
display(p1)

```

Can you try these codes? In results of these codes legends are not in appropriate place.

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [May 15, 2020, 8:33pm UTC](https://discourse.julialang.org/t/legends-on-figure/39531/8 "2020-05-15T20:33:47Z")

</div>

What behavior were you hoping to see?

Your code placed the legend outside the plot on the top left for me as expected, but -35 is way too large a legend location to be rendered properly. You would want something like (-0.1,0.9) for that location. The legend location variables are scaled such that 1.0 is the plot window size. Try (0.9,0.9) for your originally requested top-right legend location.

---

<div class="post-metadata">

**Author:** ![O\_Julia](https://avatars.discourse-cdn.com/v4/letter/o/3ab097/32.png) [@O\_Julia](https://discourse.julialang.org/u/O_Julia)\
**Post date:** [May 15, 2020, 9:03pm UTC](https://discourse.julialang.org/t/legends-on-figure/39531/9 "2020-05-15T21:03:36Z")

</div>

> [@Nathan\_Boyer](#):
>
> The legend location variables are scaled such that 1.0 is the plot window size.

I did not know this. Thank you, now I can place the legend to desired point.
