# Plot legend with multiple columns

**URL:** <https://discourse.julialang.org/t/plot-legend-with-multiple-columns/29195>\
**Category:** General Usage\
**Tags:** plotting, plots\
**Created:** [September 26, 2019, 1:15pm UTC](https://discourse.julialang.org/t/plot-legend-with-multiple-columns/29195 "2019-09-26T13:15:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![aurelio-amerio](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aurelio-amerio/32/10489_2.png) [@aurelio-amerio](https://discourse.julialang.org/u/aurelio-amerio)\
**Post date:** [September 26, 2019, 1:15pm UTC](https://discourse.julialang.org/t/plot-legend-with-multiple-columns/29195/1 "2019-09-26T13:15:18Z")

</div>

Hello, is there a way to split the labels in the legend in two columns using Plots.jl?  
I know how to do it using PyPlot (just use `legend(ncol=2)`) but I would like to avoid using python, if possible.

Thank you very much for your help!

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [September 26, 2019, 2:44pm UTC](https://discourse.julialang.org/t/plot-legend-with-multiple-columns/29195/2 "2019-09-26T14:44:45Z")

</div>

Not really with Plots.jl, but with GRUtils.jl, you might achieve this:

```julia
using GRUtils
y = sin.((0:0.1:2pi) .+ [0 0.5pi pi 1.5pi])
plot(y)
legend("\\phi = 0", "\\phi = 0.5\\pi", "\\phi = \\pi", "\\phi = 1.5\\pi",
    maxrows = 2)

```

 ![legend](https://global.discourse-cdn.com/julialang/original/3X/1/5/1544daa9405e9d70660d71c3a782b074890dd03d.png)

Notice that:

- This feature is in the master branch, but not yet released, so in the meantime you should `]add GRUtils#master`.
- This is not yet very well documented – work in progress…
- GRUtils does not use Python, but you’ll need the GR plotting library instead. Nevertheless, if you have been using Plots you’ll probably have it already installed; and if not, it should be installed and built with GRUtils via its dependency GR.jl, normally without having to do anything.
- With the keyword argument `maxrows` you are not telling how many columns you want in the legend, but the maximum number of rows allowed in each column of the legend (so the number of columns depends on that and the number of geometries you have labelled).

---

<div class="post-metadata">

**Author:** ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Post date:** [February 1, 2021, 5:51pm UTC](https://discourse.julialang.org/t/plot-legend-with-multiple-columns/29195/3 "2021-02-01T17:51:40Z")

</div>

I hit this too. I’m wondering if there is a solution to this in Plots.jl now?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [February 1, 2021, 5:59pm UTC](https://discourse.julialang.org/t/plot-legend-with-multiple-columns/29195/4 "2021-02-01T17:59:53Z")

</div>

I don’t think so, or at least I would have expected the solution to be referenced here (which tracks this amongst other issues): [Legends meta-issue · Issue #2423 · JuliaPlots/Plots.jl · GitHub](https://github.com/JuliaPlots/Plots.jl/issues/2423)

(Congrats on your prize btw!)

---

<div class="post-metadata">

**Author:** ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Post date:** [February 1, 2021, 7:06pm UTC](https://discourse.julialang.org/t/plot-legend-with-multiple-columns/29195/5 "2021-02-01T19:06:09Z")

</div>

Thanks!

I guess I’ll check out other solutions then

---

<div class="post-metadata">

**Author:** ![kongdd](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kongdd/32/17058_2.png) [@kongdd](https://discourse.julialang.org/u/kongdd)\
**Post date:** [June 30, 2021, 3:37pm UTC](https://discourse.julialang.org/t/plot-legend-with-multiple-columns/29195/6 "2021-06-30T15:37:16Z")

</div>

So, any solution for Plots.jl?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [June 30, 2021, 7:29pm UTC](https://discourse.julialang.org/t/plot-legend-with-multiple-columns/29195/7 "2021-06-30T19:29:22Z")

</div>

@kongdd, one workaround is to use [insets](https://discourse.julialang.org/t/subplots-with-one-common-legend/60020/3), but example would definitely require more plumbing:

```julia
using Plots; gr()

# define n-curves and n-labels
n = 7
x = 0:0.1:6π
yk = [k*sin.(x .- k) .* exp.(-0.02*k*x) for k in 1:n]
labels = " k = " .* string.(1:n) .* " "

# plot n-curves and legend with n-labels over 2 columns
ncolors = distinguishable_colors(n, [RGB(0,1,1), RGB(0,0,0)], dropseed=false)
n2 = ceil(Int,n/2)
b = 0.15 # adjust this function of legend sizes
p = plot(x,yk, xlabel="x", ylabel="y", color=ncolors', legend=false, widen=false)
plot!(p, (1:n2)', color=ncolors[1:n2]', inset=(1, bbox(1-2*b,0,b,1,:left)), bg=:transparent,
      subplot=2, legendfontsize=10, border=:none, label = permutedims(labels[1:n2]),
      bg_legend=:white, fg_legend=nothing, axes=false)
plot!(p, (n2+1:n)', color=ncolors[n2+1:n]', inset=(1, bbox(1-b,0, b,1,:left)), bg=:transparent,
      subplot=3, legendfontsize=10, border=:none, label = permutedims(labels[n2+1:n]),
      bg_legend=:white, fg_legend=nothing, axes=false)

```

![Plot_gr_insets_for_legends_in_2_columns](https://global.discourse-cdn.com/julialang/original/3X/8/e/8ef688a2fd78c487925dd8856fa26c8000b5a4cf.png)

---

<div class="post-metadata">

**Author:** ![briederer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briederer/32/36201_2.png) [@briederer](https://discourse.julialang.org/u/briederer)\
**Post date:** [February 1, 2023, 7:41pm UTC](https://discourse.julialang.org/t/plot-legend-with-multiple-columns/29195/8 "2023-02-01T19:41:48Z")

</div>

For those still interested in that topic:  
My PR to Plots.jl [Added arbitrary legend\_columns for gr by briederer · Pull Request #4645 · JuliaPlots/Plots.jl · GitHub](https://github.com/JuliaPlots/Plots.jl/pull/4645) that implements this feature for GR has merged today. Just need to wait for the next release.  
Maybe this helps (;

---

<div class="post-metadata">

**Author:** ![SpuriousEigenstate](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/spuriouseigenstate/32/208390_2.png) [@SpuriousEigenstate](https://discourse.julialang.org/u/SpuriousEigenstate)\
**Post date:** [April 19, 2024, 3:10pm UTC](https://discourse.julialang.org/t/plot-legend-with-multiple-columns/29195/9 "2024-04-19T15:10:12Z")

</div>

So indeed, setting `legend_columns=2` now works.
