# Subplots with one common legend

**URL:** <https://discourse.julialang.org/t/subplots-with-one-common-legend/60020>\
**Category:** Visualization\
**Created:** [April 26, 2021, 8:18am UTC](https://discourse.julialang.org/t/subplots-with-one-common-legend/60020 "2021-04-26T08:18:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmetivie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmetivie/32/6926_2.png) [@dmetivie](https://discourse.julialang.org/u/dmetivie)\
**Post date:** [April 26, 2021, 8:18am UTC](https://discourse.julialang.org/t/subplots-with-one-common-legend/60020/1 "2021-04-26T08:18:57Z")

</div>

I am trying to plot many subplots with one common legend that is not on the subplots but in between the two subplot rows where it is empty. Moreover, it should not enlarge the space between the row and make the plot unnecessary larger.

```julia
using Plots
pyplot();
D = 10
K = 4
θ = range(0, stop=2π, length=50)
r(θ) = 1 + cos(θ) * sin(θ)^2
p = [plot(proj=:polar) for j in 1:D]
[[plot!(p[j], θ, r.(θ)+0.2*rand(length(θ)), label =:none) for k in 1:K] for j in 1:D]
plot(p...,size=(1200,600), layout = (2,5))

```

I would like to achieve something like that

 ![image](https://global.discourse-cdn.com/julialang/original/3X/c/5/c558832b2301e39b7082f0391d977a6698707af2.jpeg)

---

<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:** [April 26, 2021, 10:18am UTC](https://discourse.julialang.org/t/subplots-with-one-common-legend/60020/2 "2021-04-26T10:18:19Z")

</div>

This seems to have already been addressed in this [post](https://discourse.julialang.org/t/subplots-in-plots-jl-with-separate-legend/1968/9).

---

<div class="post-metadata">

**Author:** ![dmetivie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmetivie/32/6926_2.png) [@dmetivie](https://discourse.julialang.org/u/dmetivie)\
**Post date:** [April 27, 2021, 9:27am UTC](https://discourse.julialang.org/t/subplots-with-one-common-legend/60020/3 "2021-04-27T09:27:42Z")

</div>

Thanks for the link. Unfortunatly if I understand, the solution was to change the layout to add an empty subplot with only the legend. Here I don’t want to touch my layout. I just want to superpose the legend to the grid of subplots.

I think I found my answer manipulating inset plots. It just adds one line, so I think it is more robust than changing the layout, and you really can put the legend anywhere on the plot. (Drawback you might have to adjust manually the position).

```julia
D = 10
K = 4
θ = range(0, stop=2π, length=50)
r(θ) = 1 + cos(θ) * sin(θ)^2
p = [plot(proj=:polar) for j in 1:D]
[[plot!(p[j], θ, r.(θ)+0.2*rand(length(θ)), label =:none) for k in 1:K] for j in 1:D]
pall = plot(p...,size=(1200,600), layout = (2,5))
plot!(pall, (1:K)', inset = (1, bbox(2.25,0.8,0.3,0.3)), background_color=:transparent, subplot = D+1, legendfontsize=11, framestyle = :none, label=permutedims("k=".*string.(1:K)))

```

I am not exactly sure how to control the size of the legend box (it does not change with bbox) but with ‘legendfontsize’ and ‘background\_color’ it is perfect for my application.

---

<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:** [April 27, 2021, 9:48am UTC](https://discourse.julialang.org/t/subplots-with-one-common-legend/60020/4 "2021-04-27T09:48:17Z")

</div>

Excellent.

_ **NB:** by editing the last line as follows the legend looks better on my display if_ `using Plots; gr()`:

```julia
plot!(pall, (1:K)', inset = (1, bbox(1.65,0.87,1.0,1.0)), background_color=:transparent,
      subplot=D+1,legendfontsize=11,framestyle=:none,label=permutedims(" k=".*string.(1:K).*" "))

```

_and if one wants to remove the border of the inset:_

```julia
plot!(pall, (1:K)', inset = (1, bbox(1.65,0.87,1.0,1.0)), background_color=:transparent,
      subplot=D+1,legendfontsize=11,framestyle=:none,label=permutedims(" k=".*string.(1:K).*" "),
      foreground_color_legend = nothing)
```
