Subplots in Plots.jl with separate legend



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:

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)
    DataFrame(year=years, spelltype=spelltypes, value=values)

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


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


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.


@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> pyplot()
INFO: Recompiling stale cache file /home/tamas/.julia/lib/v0.5/PyPlot.ji for module

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?


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


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


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