Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Add a legend with checkboxes to a plot that toggles individual datasets on and off dynamically
ResourceFunction["CheckboxLegended"][func[<|lbl1→ item1,lbl2→item2,…|>,…]] returns the plot generated by func with plot legends that include checkboxes for showing and hiding individual plotted items. | |
ResourceFunction["CheckboxLegended"][func[…],Dynamic[var]] links the visibility of the datasets to variable var. |
Add a Checkbox legend to a ListPlot:
In[1]:= | ![]() |
Out[2]= | ![]() |
The options of ListPlot can still be used to specify your own styles:
In[3]:= | ![]() |
Out[3]= | ![]() |
Use it with Plot:
In[4]:= | ![]() |
Out[4]= | ![]() |
With BubbleChart:
In[5]:= | ![]() |
Out[5]= | ![]() |
Link the visibility of datasets to a variable:
In[6]:= | ![]() |
Out[6]= | ![]() |
Other data wrappers will remain functional:
In[7]:= | ![]() |
Out[8]= | ![]() |
Use the "LegendLayoutFunction" option to style the legend:
In[9]:= | ![]() |
Out[9]= | ![]() |
Normally, Plot localizes variables:
In[10]:= | ![]() |
Out[11]= | ![]() |
CheckboxLegended evaluates the plotting data before Plot, thereby undoing the localization:
In[12]:= | ![]() |
Out[13]= | ![]() |
Use Block to circumvent this issue:
In[14]:= | ![]() |
Out[15]= | ![]() |
If you don’t specify the linking variable, CheckboxLegended will return a DynamicModule that cannot be combined with other graphics using Show:
In[16]:= | ![]() |
Out[16]= | ![]() |
The output is a DynamicModule:
In[17]:= | ![]() |
Out[17]= | ![]() |
Therefore, Show cannot combine it with other graphics:
In[18]:= | ![]() |
Out[18]= | ![]() |
To combine graphics, use the optional second argument:
In[19]:= | ![]() |
Out[19]= | ![]() |
When combining multiple wrappers, make sure that Style is on the outside. The checkboxes will not work if the Style wrapper is deeper inside:
In[20]:= | ![]() |
Out[21]= | ![]() |
It will work when it’s the outermost wrapper:
In[22]:= | ![]() |
Out[22]= | ![]() |
Wolfram Language 11.3 (March 2018) or above
This work is licensed under a Creative Commons Attribution 4.0 International License