How to change legends place with Plots, gr()?

question
plotting

#1

I’m using Plots and GR, and can’t find a way to change the legends location in a plot. As I understand currently I can’t do it with Plots, there’s no keyword argument for it (correct me if I’m wrong).
Can someone please shoew me a method how to do that? I would prefer a solution with Plots with GR backend.


#2
using Plots
plot(randn(10,3),legend=:bottomleft)

Possible keywords are :right, :left, :top, :bottom, :inside, :best, :legend, :topright, :topleft, :bottomleft, :bottomright

:outertopright not yet supported :frowning: (in GR)


#3

Tank you! That is what I’m looking for.


#4

Is there support for specifying number of columns in the legend list? (Like in PyPlot…)


#5

No


#6

Does any know whether there are any efforts underway to allow legends to be placed outside the plot? Unfortunately, placing the legend within the plot often obscures the data.


#7

If you use PyPlot.jl directly, it is possible with something like:

legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.0)

#8

Thank you. That might suffice as a workaround. Do you have any idea if its is possible to access that method via Plots using low level functions or something of that nature? The reason I ask is that using PyPlot directly would require rewriting a fair amount of code. Thanks.


#9

I am no expert in this but this seems to work:

using Plots; pyplot()
pt = plot(1:10, 1:10, label="A", legend=false)
pt.o[:legend](bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.0)

pt.o seems to give the plot’s PyObject under the hood so you can work with it much like any other PyObject from PyCall.jl. Of course all valid matplotlib.pyplot functions are also valid in this context using PyCall syntax.

When you display pt, it will not show the legend, but if you display pt.o, it will show.


#10

Nice solution. Thanks!