On some backends (plotly,plotlyjs), the following code combines the legends into a single one, while on some backends the (gr, pyplot) the legends are separate for each subplot (as they should be). Cf discussion on Discourse. Same with master and release versions (at the time of opening the issue).
using DataFrames
using Plots
using StatPlots
plotly()
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
I think the placement of legends in combined plots is sub-optimal right now. Using :bottom, :right, :top, and others produce weird output at least with gr. It would be nice to allow a :shared keyword or something to combine the legend as well.
I second Nosferican's suggestion for a :shared keyword
yes it would be nice to have for sure
In the absence of that, any suggestions for how I would get it using pyplot() backend?
I think the placement of legends in combined plots is sub-optimal right now
You can also pass an (x,y) Tuple for fine-grained control.
true, but I can't (easily) get the legend outside of a subplot right? So for example I've got a 6 by 2 layout and would ideally have the legend sitting centred above the top row of plots.
Colorbar or legend? There are some hacks - with colorbar you can set the clims then optionally use the @layout macro to align widths:
a,b,c,d = ntuple(x->randn(10000), 4)
p1 = histogram2d(a,b, colorbar = false)
p2 = histogram2d(c,d, clims = zlims(p1))
plot(p1, p2, layout = @layout([a{0.46w} b]))

With legend you can always use an invisible subplot and hack it a bit:
plot(
scatter(rand(100,3), legend = false),
scatter(rand(100,3), legend = false),
scatter((1:3)', xlim = (4,5), legend = true, framestyle = :none),
layout = @layout([a b c{0.1w}]))

But actually, improvements to the general legend capability are much in demand
I'm encountering this legend issue with my multi-panel plot (some of the subplots are heatmaps) using Plotly as my backend. A coherent solution to this problem allowing for separate legends for each subplot is very appreciated.
This is a longstanding issue with plotly, and it is really due to plotlys inability to have multiple legends.
I would also like to have the option to display just one legend for all subplots on GR backend. I would like to use a separate keyword plot_legend instead of legend for that, which is logically similar to how plot_title and title should behave (although plot_title is not implemented currently).
Most helpful comment
I think the placement of legends in combined plots is sub-optimal right now. Using
:bottom,:right,:top, and others produce weird output at least withgr. It would be nice to allow a:sharedkeyword or something to combine the legend as well.