Legends on figure

I am using Plots.jl with plotly() backend. Legends of following figure are outer of the figure.

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


Thank you.

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.

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

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

Producing the following:



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

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?

1 Like

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

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

using Plots

x = collect(range(-64,64,length=400));
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)

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

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.

1 Like

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