Add one legend to layout plot in Julia

I would like to create one legend for a layout plot in Julia. Here is some reproducible code:

using Plots plot(rand(100, 4), layout = 4, color = [:red :blue :green :yellow]) 

Output:

enter image description here

As you can see it shows nicely a legend to each subplot, but I would like to have one shared legend, using :shared doesn't work. So I was wondering if anyone knows how to add a shared legend to a layout plot in Julia?

1 Answer

Maybe this answer can give an idea. I can do it by a trick (creating a transparent plot for the legend). The only problem is that I don't know how I can move the legend (actually, the transparent plot) to the location I want:

colors = [:red :blue :green :yellow] p = [ plot(col, legend=false, color=colors[idx]) for (idx, col) in enumerate(eachcol(data)) ] p1 = plot((1:4)', legend=true, framestyle=:none, color=colors) plot( p..., p1, layout = @layout[ [ grid(2, 2) p ] p1 ], size = (1000, 1000) ) 

result of savefig: enter image description here

If I change the layout like the following:

layout = @layout[ p1 [ grid(2, 2) p ] ] # And change the order of the plots plot( p1, p..., ⋮ ) 

Then I get this: enter image description here

Note that I cropped the redundant area of the image above.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like