# Subplots in Plots.jl with separate legend

**URL:** <https://discourse.julialang.org/t/subplots-in-plots-jl-with-separate-legend/1968>\
**Category:** Visualization\
**Tags:** question, plots\
**Created:** [February 7, 2017, 9:09pm UTC](https://discourse.julialang.org/t/subplots-in-plots-jl-with-separate-legend/1968 "2017-02-07T21:09:21Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 7, 2017, 9:09pm UTC](https://discourse.julialang.org/t/subplots-in-plots-jl-with-separate-legend/1968/1 "2017-02-07T21:09:21Z")

</div>

I am plotting 20+ timeseries with various scales from a `DataFrame` using `Plots.jl`, separated into subplots because the original plot is too crowded. Everything works nicely, but I would like a separate legend (with only the plotted series) for each subplot. Self-contained **toy example** :

```julia
using DataFrames
using Plots
using StatPlots

data = let years=[], spelltypes=[], values=[]
    for year in 1980:2000
        for spelltype in 1:30
            push!(values, spelltype+(rand()-0.5)/10)
            push!(years, year)
            push!(spelltypes, spelltype)
        end
    end
    DataFrame(year=years, spelltype=spelltypes, value=values)
end

pick(data, range) = data[[spelltype ∈ range for spelltype in data[:spelltype]],:]
subplot(data, range) = plot(pick(data, range), :year, :value, group=:spelltype)

p1 = subplot(data, 1:10)
p2 = subplot(data, 11:20)
p3 = subplot(data, 21:30)
plot(p1, p2, p3) # would like separate legend for each subplot

```

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [February 7, 2017, 9:56pm UTC](https://discourse.julialang.org/t/subplots-in-plots-jl-with-separate-legend/1968/2 "2017-02-07T21:56:38Z")

</div>

Funny, I get separate legends in each subplot when trying that code

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 7, 2017, 10:33pm UTC](https://discourse.julialang.org/t/subplots-in-plots-jl-with-separate-legend/1968/3 "2017-02-07T22:33:31Z")

</div>

Yeah separate legends is easy and automatic. Did you mean you didn’t want separate legends and just wanted a master legend? That’s difficult and I think I have an issue open for that.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 8, 2017, 7:49am UTC](https://discourse.julialang.org/t/subplots-in-plots-jl-with-separate-legend/1968/4 "2017-02-08T07:49:46Z")

</div>

@mkborregaard, @ChrisRackauckas: now I have tried various backends. I get separate legends in `gr()`, but a single legend in `plotly()` and `plotlyjs()`, and an error for `pyplot()`:

```julia
julia> pyplot()
INFO: Recompiling stale cache file /home/tamas/.julia/lib/v0.5/PyPlot.ji for module
 PyPlot.                                                                          
Plots.PyPlotBackend()

julia> plot(p1, p2, p3) # would like separate legend for each subplot
Error showing value of type Plots.Plot{Plots.PyPlotBackend}:
ERROR: MethodError: no method matching py_init_subplot(::Plots.Plot{Plots.PyPlotBac
kend}, ::Plots.Subplot{Plots.PlotlyBackend})                                      
Closest candidates are:
  py_init_subplot(::Plots.Plot{Plots.PyPlotBackend}, ::Plots.Subplot{Plots.PyPlotBa
ckend}) at /home/tamas/.julia/v0.5/Plots/src/backends/pyplot.jl:393               

```

Using released versions of all packages, and `0.5.0`.

Is the above not the syntax I should be using?

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [February 8, 2017, 9:03am UTC](https://discourse.julialang.org/t/subplots-in-plots-jl-with-separate-legend/1968/5 "2017-02-08T09:03:01Z")

</div>

AFAICS there is nothing wrong with your syntax. It is strange. I am on Plots master and PyPlot release.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 8, 2017, 9:31am UTC](https://discourse.julialang.org/t/subplots-in-plots-jl-with-separate-legend/1968/6 "2017-02-08T09:31:24Z")

</div>

I tried master for `Plots`, `PlotlyJS` and `Plotly`, and I get the combined legend. With master `PyPlot`, I do get the separate legends.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [February 8, 2017, 9:32am UTC](https://discourse.julialang.org/t/subplots-in-plots-jl-with-separate-legend/1968/7 "2017-02-08T09:32:37Z")

</div>

I think the best course of action is to open an issue on Plots

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 8, 2017, 9:42am UTC](https://discourse.julialang.org/t/subplots-in-plots-jl-with-separate-legend/1968/8 "2017-02-08T09:42:04Z")

</div>

[https://github.com/JuliaPlots/Plots.jl/issues/673](https://github.com/JuliaPlots/Plots.jl/issues/673)

---

<div class="post-metadata">

**Author:** ![svpillai](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@svpillai](https://discourse.julialang.org/u/svpillai)\
**Post date:** [January 15, 2019, 10:09am UTC](https://discourse.julialang.org/t/subplots-in-plots-jl-with-separate-legend/1968/9 "2019-01-15T10:09:19Z")

</div>

Apologies for reopening an older post, but I have a panel of plots (using Plots.jl) and specifying legend=true at the end while specifying the layout makes it very messy and shows giant legends that cover up most of the subplots. I’d like to specify a master legend i.e. one legend for all subplots, how would I do that?  
Thank you!

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [January 15, 2019, 10:42am UTC](https://discourse.julialang.org/t/subplots-in-plots-jl-with-separate-legend/1968/10 "2019-01-15T10:42:03Z")

</div>

You can always hack it together with `layout` and an extra subplot.

```julia
plot(
    plot(rand(100,3), legend = false), 
    plot((1:3)', legend = true, framestyle = :none)
    )

```

---

<div class="post-metadata">

**Author:** ![svpillai](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@svpillai](https://discourse.julialang.org/u/svpillai)\
**Post date:** [January 15, 2019, 3:30pm UTC](https://discourse.julialang.org/t/subplots-in-plots-jl-with-separate-legend/1968/11 "2019-01-15T15:30:53Z")

</div>

So the issue here is that I have originally 15 subplots, and doing “layout=(5,3)” fits the graphs well, however if I add the master legend bit as an extra subplot (I had tried doing this earlier myself), it shows an error saying “When doing layout, n(15) != n\_override(16). You’re probably trying to force existing plots into a layout that doesn’t fit them.” I’ve tried playing around with the layout (i.e. I’ve tried tuples of (8,2) to fit the legend subplot but this layout highly distorts the graphs, or layout=(6,3) to accommodate for the extra graph), but it keeps showing the error message, which means that Plots.jl requires exactly the same correct number of subplots in total as specified in the layout. Is there a workaround?

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [January 15, 2019, 3:46pm UTC](https://discourse.julialang.org/t/subplots-in-plots-jl-with-separate-legend/1968/12 "2019-01-15T15:46:28Z")

</div>

You can use a 4x4 layout. Leave the 16th empty, then plot to it with `plot!(...somearguments..., subplot = 16, framestyle = :none)`

---

<div class="post-metadata">

**Author:** ![svpillai](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@svpillai](https://discourse.julialang.org/u/svpillai)\
**Post date:** [January 15, 2019, 4:59pm UTC](https://discourse.julialang.org/t/subplots-in-plots-jl-with-separate-legend/1968/13 "2019-01-15T16:59:23Z")

</div>

this is perfect, thank you!
