clickableMultiLegend

clickableMultiLegend hides/shows symbols inside all gplotmatrix subplots (or similar multi-plots) clicking on the legend.

Syntax

  • varargout=clickableMultiLegend()example
  • varargout=clickableMultiLegend(Name,Value)example

Description

It is typically applied to gplotmatrix figures. By clicking on a text label in the legend, the graphics (line or patch) objects associated to that label in all subplots are turned on and off (hide/show).

The extention to multiple plots is realised by looking for graphics objects with the same DisplayName property of the one associated to the legend label. Therefore, the function should work also through plots in different figures.

clickableMultiLegend accepts the same parameters of the legend function and can be used in the same way.

example

varargout =clickableMultiLegend() ClickableMultilegend applied to a plotmatrix without groups.

example

varargout =clickableMultiLegend(Name, Value) ClickableMultilegend applied to a plotmatrix without groups.

Examples

expand all

  • ClickableMultilegend applied to a plotmatrix without groups.
  • In this case, ClickableMultilegend has no effect on the plot.

    gplotmatrix(randn(50,2));
    clickableMultiLegend;

  • ClickableMultilegend applied to a plotmatrix without groups.
  • In this case, the argument is used to give a name to the units of the group.

    gplotmatrix(randn(50,2));
    clickableMultiLegend('The units in the plot','FontSize',14);

    Related Examples

    expand all

  • ClickableMultilegend applied to a single scatter with several groups.
  • Simulate 3 groups

    n1=30; n2=20; n3=50;
    y = [rand(n1,1); rand(n2,1)+1; rand(n3,1)+2];
    group= [2*ones(n1,1); ones(n2,1); zeros(n3,1)];
    X = rand(n1+n2+n3,1);
    gscatter(X,y,group);
    % Make the legends clickable
    clickableMultiLegend();

  • clickableMultiLegend applied to two plots with lines with same legend names.
  • Clicking on a legend line, will show/hide the selected line in both plots.

    z = peaks(100);
    plot(z(:,26:5:50),'LineWidth',3)
    grid on;
    % define the names of the legend labels for both plots
    names = {'Line1','Line2','Line3','Line4','Line5'};
    clickableMultiLegend(names , 'Location', 'NorthWest','FontSize',14);
    figure;
    plot(z(:,32:5:56),'LineWidth',3)
    grid on;
    hlegend=clickableMultiLegend(names, 'Location', 'NorthWest','FontSize',14);

  • clickableMultiLegend applied without arguments to multiple subplots.
  • % Simulate X and y with 3 groups
    X = rand(100,4);
    y = [rand(10,1); rand(20,1)+1; rand(70,1)+2];
    group= [2*ones(10,1); ones(20,1); zeros(70,1)];
    % Generate the scatter matrix
    gplotmatrix(X,y,group);
    % Without arguments, the effect is on the default legend
    clickableMultiLegend();

  • clickableMultiLegend applied with legend style arguments to multiple subplots.
  • % Simulate X and y with 3 groups
    X = rand(100,4);
    y = [rand(10,1); rand(20,1)+1; rand(70,1)+2];
    group= [2*ones(10,1); ones(20,1); zeros(70,1)];
    % Generate the scatter matrix
    gplotmatrix(X,y,group);
    % Now we just want to change the font size of the legend
    clickableMultiLegend('FontSize',14);

  • clickableMultiLegend applied with legend names to multiple subplots.
  • % Simulate X and y with 3 groups
    X = rand(100,4);
    y = [rand(10,1); rand(20,1)+1; rand(70,1)+2];
    group= [2*ones(10,1); ones(20,1); zeros(70,1)];
    % Generate the scatter matrix
    gplotmatrix(X,y,group);
    % Update the legend and make them clickable
    clickableMultiLegend({'group 1' , 'group 2' , 'group 3'},'FontSize',14);

  • clickableMultiLegend applied with the handles of the line objects in multiple subplots.
  • % This example shows that clickableMultiLegend can also receive the 
    % handles returned by a multiple panel figure such as gplotmatrix.
    % Simulate X and y with 3 groups
    X = rand(100,4);
    y = [rand(10,1); rand(20,1)+1; rand(70,1)+2];
    group= [2*ones(10,1); ones(20,1); zeros(70,1)];
    % Generate the scatter matrix
    [H,AX,bigax] = gplotmatrix(X,y,group);
    % Set the DisplayName property (i.e. the texts of the legend) in all panels.
    % Note that in the gplotmatrix only one legend is visible.
    set(H(:,:,1),'DisplayName','group 10');
    set(H(:,:,2),'DisplayName','group 20');
    set(H(:,:,3),'DisplayName','group 30');
    % Get the handles of the legend to update
    hLines = findobj(AX(1,end), 'type', 'line');
    % Update the legend and make them clickable
    clickableMultiLegend(hLines,'FontSize',14);

  • clickableMultiLegend again with the line objects handles, with a change in the legend order.
  • % Simulate X and y with 3 groups
    X = rand(100,4);
    y = [rand(10,1); rand(20,1)+1; rand(70,1)+2];
    group= [2*ones(10,1); ones(20,1); zeros(70,1)];
    % Generate the scatter matrix
    [H,AX,bigax] = gplotmatrix(X,y,group);
    % Get the new legend texts directly from the plot
    % (takes into account a change in the legend property names in 2016b)
    if verLessThan('matlab','9.1')
    legstring='LegendPeerHandle';
    else
    legstring='LayoutPeers';
    end
    leg = get(getappdata(AX(1,end),legstring),'String');
    % Get the handles of the legend to update
    hLines = findobj(AX(1,end), 'type', 'line');
    % Change the order of the legend strings, for exaple by sorting
    hLines = sort(hLines);
    % Update the legend and make them clickable
    clickableMultiLegend(hLines, leg{:},'FontSize',14);

  • clickableMultiLegend again with the line objects handles, with changes in the legend text.
  • % Here we make a gplotmatrix plot clickable, then we change the labels
    % after a convenient reshape of its handles array: while H is a 3-dimensional
    % array with the third dimension associated to the groups, newH is 2-dimensional
    % with lines associated to the subplots of the scatterplot and columns
    % associated to the groups. This simplifies the redefinition of the
    % DisplayName property.
    % Simulate X and y with 3 groups
    X = rand(100,4);
    y = [rand(10,1); rand(20,1)+1; rand(70,1)+2];
    group= [2*ones(10,1); ones(20,1); zeros(70,1)];
    % Generate the scatter matrix
    [H,AX,bigax] = gplotmatrix(X,y,group);
    % make the legend clickable
    clickableMultiLegend('FontSize',14);
    % reshape the line handles array
    hLines = findobj(AX(1,end), 'type', 'line');
    nleg = numel(hLines);
    newH = reshape(H,numel(H)/nleg,nleg);
    % redefine the legend texts
    for i = 1 : nleg
    set(newH(:,i),'DisplayName',['Redefined group n. ' num2str(i)]);
    end
    % If the legend texts were clickable before the re-definition, they
    % will remain clickable.

    Input Arguments

    expand all

    Name-Value Pair Arguments

    Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

    Example:

    Output Arguments

    expand all

    varargout —HLEG : handle to legend. Graphics handle

    This is the handle to legend on the current axes or empty if none exists.

    References

    Deoras A. (2008), http://www.mathworks.com/matlabcentral/fileexchange/21799-clickablelegend-interactive-highlighting-of-data-in-figures/content/ href="clickableLegend.html">clickableLegend [clickableMultiLegend extends the clickableLegend by Ameya Deoras to figures with one legend for several subplots].

    See Also

    |

    This page has been automatically generated by our routine publishFS