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

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.

5 Likes
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)

32 Likes

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

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

No

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.

3 Likes

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

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

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.

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.

1 Like

Nice solution. Thanks!

I tried to run this code but I get the following error message:

No handles with labels found to put in legend.

I’m using Jupyter with Julia 1.0.1. Do you have any idea what I might be doing wrong?

I am not getting your error, but it doesn’t seem to be working anymore. I am not sure what’s wrong and I don’t have time to dig into it, sorry. Someone else may be more useful.

Hi,

I saw you post here. I’m encountering the same problem. Have you fixed it? Many thanks in advance.

Encountered the same problem with gr(). I haven’t tried yet, but check this post. julia - How to move the legend to outside the plotting area in Plots.jl (GR)? - Stack Overflow

It would be desirable to have this functionality enabled in the gr backend without the layout hack mentioned on stackoverflow

Should exist now. https://github.com/JuliaPlots/Plots.jl/pull/2018

7 Likes

:outerbottom and :outertoporight etc are supported now!

7 Likes

When I use legend=:bottomright the legend is not placed close enough to the bottom (see picture). Is there a way to make it move more to the bottom so it won’t hide the data?

2 Likes

Same happens with :topright and :topleft. The legend is placed too far away from the top border overlapping many times the data in the plot. Moving the legend up for the top ones and down for the bottom ones will be helpful.

1 Like

Should this be able to work with heatmap as well? It does not appear so from my trials