After successfully converting all of my plotting functions to actual Makie
recipes, I am now wondering if there is an official way to provide legends for custom plots.
I know that at the moment, recipes cannot modify the surrounding layout / axis / figure, but would it be possible to at least build a list of LegendElement
s or else for the recipe ?
This part of the API hasn’t really been worked out that well yet, because of the general issue around layout / axis / figure access that you mention. There is a function which is not further mentioned in the docs, Makie.get_plots
which was introduced at some point so that the series
recipe can by default have a legend with multiple entries. You can overload that for your recipe type so it returns the subplots that you want, and if they have label
attributes things should generally work.
If you’re instead talking about a special symbol or marker for your plot type, that’s better defined and is done via the legendelements
function which you can overload for your plot type as well. From that you can return whatever LegendElements
you want
Well I’ve trie both get_plots
and legendelements
but makie still complains about plots not having labels (though these are provided to the underlying plots) and does not show any legend.
Would you have a good example of a use case of legendelements
? There does not seem to be any makie documentation about this function yet, or did I miss something ?
No it’s not further documented. I’d look in the source for places where it’s been defined for Makie’s own plots, that should tell you what it does.
But can you give an example what your recipe looks like and what legend you want?
Sorry for the long delay !
To sum up my situation, I have two main plotting recipes that both use several custom recipes for plotting lower level objects
I finally managed to get legendelements
working for all of the underlying objects, resulting in a correct vector of LegendElement
s.
However what I want in the legend for my main recipes are just the list of legend of each lower level plot. For that purpose, I tried
f,ax,p = mymainrecipe(args)
Legend(f[1,2],ax,["one","label","per","lower","level","plot"])
But that fails.
What I did instead is
Legend(f[1,2],p.plots,["one","label","per","lower","level","plot"])
which works fine !
Well I’ve trie both
get_plots
andlegendelements
but makie still complains about plots not having labels (though these are provided to the underlying plots) and does not show any legend.
IIUC then the issue is that there are no fallbacks for legendelements
for all plot types.
The reason there are none is that it is not yet decided on what should be the marker for every plot type (e.g. what do you put next to the label in the legend when the plot is a heatmap with a gradient color scheme).
A simple solution would be to ignore markers in a fallback and only put the label into the legend. I suggested something along those lines here: `density` with gradient color cannot be added to `Legend` · Issue #2289 · MakieOrg/Makie.jl · GitHub