Common legend using layouts

Dear all,

I am plotting this:

plot(rand(32,10), layout = (2,5), legend=false)
plot!(rand(32,10), layout = (2,5), legend=false)

And I’d like to have just one legend saying what the blue lines stand for and what the orange lines stand for. Anybody knows how to make it happen?

If you don’t mind having the legend inside the first plot, you can do:

plot(layout=(2,5))
plot!(rand(32),subplot=1,label="blue")
plot!(rand(32),subplot=1,label="orange")
for sp in 2:10
   plot!(rand(32),subplot=sp,label="")
end
plot!()

If you do mind, there are sophisticated plot layouts that can be obtained using Makie.jl

1 Like

I usually end up building subplots from individual plots because I find that various settings differ by subplot (e.g., only showing x axis labels on the plots in the bottom row; only showing y axis labels for the left most column).
So I do:

pV = Vector{Any}(undef, n);
for j = 1 : n
  # can customize axis labels etc here
  pV[j] = plot(<data>, labels = ["blue" "red'], legend = (j == 1));
end
p = plot(pV..., layout = (2,5));
1 Like

Those are useful. I’ve end up doing something similar. However, it’s not quite what I was looking for. I wanted one common legend, to be placed for example outside the plot region, common for all subplots. That seems not to be possible.

The Makie package provides tools to do almost anything. For example:

http://juliaplots.org/MakieReferenceImages/gallery//faceting/index.html

Other examples are available at: http://juliaplots.org/MakieReferenceImages/gallery/index.html