{"id":6691,"date":"2016-03-25T09:00:55","date_gmt":"2016-03-25T13:00:55","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=6691"},"modified":"2016-03-25T08:39:21","modified_gmt":"2016-03-25T12:39:21","slug":"interactive-legend-in-r2016a","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2016\/03\/25\/interactive-legend-in-r2016a\/","title":{"rendered":"Interactive Legend in R2016a"},"content":{"rendered":"\n<div class=\"content\"><p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is a feature that allows you to <a title=\"https:\/\/www.mathworks.com\/help\/releases\/R2016a\/matlab\/creating_plots\/create-callbacks-for-interacting-with-legend-items.html (link no longer works)\">create interactive legends using callbacks<\/a>.<\/p><p>This week, I'd like to highlight one of the new <a href=\"https:\/\/www.mathworks.com\/products\/new_products\/latest_features.html?s_tid=hp_spot_R2016a_0316\">R2016a features<\/a> that just came out a couple of weeks ago. There are so many exciting <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/release-notes.html#R2016a\">features<\/a>, and the one I'll be talking about today is related to a <a href=\"https:\/\/blogs.mathworks.com\/pick\/2008\/10\/31\/clickablelegend\/\">Pick<\/a> from a while ago on <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/21799-clickablelegend-interactive-highlighting-of-data-in-figures\"><tt>clickableLegend<\/tt><\/a> by one of our ex-MathWorkers, <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/6055857\">Ameya<\/a>. Ameya's entry allowed you to create an interactive legend for turning on and off particular lines.<\/p><p>With R2016a, you can specify custom actions that get executed when one clicks on a legend item. Let's try with the following data set.<\/p><pre class=\"codeinput\">x = linspace(0,10);\ny1 = sin(x);\ny2 = cos(x);\ny3 = sin(x) + cos(x);\ny4 = sin(x) .* cos(x);\nplot(x,y1,x,y2,x,y3,x,y4)\n<\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_customlegend\/custom_legend_initial_plot.png\" alt=\"\"> <\/p><p><b>Toggle Visibility<\/b><\/p><p>To use this new feature, you first create a function that defines the specific action you want to perform. For example, the following function toggles the visibility of the line.<\/p><pre class=\"language-matlab\"><span class=\"keyword\">function<\/span> action1(src,event)\n<span class=\"comment\">% This callback toggles the visibility of the line<\/span>\n\n<span class=\"keyword\">if<\/span> strcmp(event.Peer.Visible,<span class=\"string\">'on'<\/span>)   <span class=\"comment\">% If current line is visible<\/span>\n    event.Peer.Visible = <span class=\"string\">'off'<\/span>;      <span class=\"comment\">%   Set the visibility to 'off'<\/span>\n<span class=\"keyword\">else<\/span>                                 <span class=\"comment\">% Else<\/span>\n    event.Peer.Visible = <span class=\"string\">'on'<\/span>;       <span class=\"comment\">%   Set the visibility to 'on'<\/span>\n<span class=\"keyword\">end<\/span>\n<\/pre><p>Refer to the <a title=\"https:\/\/www.mathworks.com\/help\/releases\/R2016a\/matlab\/ref\/legend-properties.html#property_itemhitfcn (link no longer works)\"><tt>ItemHitFcn<\/tt><\/a> property for the explanation of the <tt>event<\/tt> data structure.<\/p><p>Then, you assign the function to the <tt>ItemHitFcn<\/tt> property of the <a title=\"https:\/\/www.mathworks.com\/help\/releases\/R2016a\/matlab\/ref\/legend.html (link no longer works)\"><tt>legend<\/tt><\/a> object.<\/p><pre class=\"codeinput\">hLeg = legend(<span class=\"string\">'Line 1'<\/span>,<span class=\"string\">'Line 2'<\/span>,<span class=\"string\">'Line 3'<\/span>,<span class=\"string\">'Line 4'<\/span>);\nhLeg.ItemHitFcn = @action1;\n<\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_customlegend\/custom_legend_1.gif\" alt=\"\"> <\/p><p><b>Blinking Line<\/b><\/p><p>Here is a different function that blinks the line that is clicked.<\/p><pre class=\"language-matlab\"><span class=\"keyword\">function<\/span> action2(src,event)\n<span class=\"comment\">% This callback causes the line to \"blink\"<\/span>\n\n<span class=\"keyword\">for<\/span> id = 1:3                        <span class=\"comment\">% Repeat 3 times<\/span>\n    event.Peer.LineWidth = 3;       <span class=\"comment\">% Set line width to 3<\/span>\n    pause(0.2)                      <span class=\"comment\">% Pause 0.2 seconds<\/span>\n    event.Peer.LineWidth = 0.5;     <span class=\"comment\">% Set line width to 0.5<\/span>\n    pause(0.2)                      <span class=\"comment\">% Pause 0.2 seconds<\/span>\n<span class=\"keyword\">end<\/span>\n<\/pre><pre class=\"codeinput\">hLeg.ItemHitFcn = @action2;\n<\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_customlegend\/custom_legend_2.gif\" alt=\"\"> <\/p><p><b>Displaying Line Elsewhere<\/b><\/p><p>Here is another function that displays the line on a different set of axes.<\/p><pre class=\"language-matlab\"><span class=\"keyword\">function<\/span> action3(src,event,hAx)\n<span class=\"comment\">% This callback displays the selected line on a different set of axes<\/span>\n\nx = event.Peer.XData;                   <span class=\"comment\">% Get X data of interest<\/span>\ny = event.Peer.YData;                   <span class=\"comment\">% Get Y data of interest<\/span>\nplot(hAx,x,y,<span class=\"string\">'Color'<\/span>,event.Peer.Color)  <span class=\"comment\">% Plot data with the same color<\/span>\ntitle(hAx,event.Peer.DisplayName)       <span class=\"comment\">% Set the title to the line name<\/span>\n<\/pre><pre class=\"codeinput\">subplot(2,1,1)\nplot(x,y1,x,y2,x,y3,x,y4)\nhLeg = legend(<span class=\"string\">'Line 1'<\/span>,<span class=\"string\">'Line 2'<\/span>,<span class=\"string\">'Line 3'<\/span>,<span class=\"string\">'Line 4'<\/span>);\nhAx = subplot(2,1,2);\nhLeg.ItemHitFcn = @(src,event) action3(src,event,hAx);\n<\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_customlegend\/custom_legend_3.gif\" alt=\"\"> <\/p><p><b>Comments<\/b><\/p><p>Let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=6691#respond\">here<\/a>.<\/p><script language=\"JavaScript\"> <!-- \n    function grabCode_750d0863927f46519ce50d401f38cded() {\n        \/\/ Remember the title so we can use it in the new page\n        title = document.title;\n\n        \/\/ Break up these strings so that their presence\n        \/\/ in the Javascript doesn't mess up the search for\n        \/\/ the MATLAB code.\n        t1='750d0863927f46519ce50d401f38cded ' + '##### ' + 'SOURCE BEGIN' + ' #####';\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 750d0863927f46519ce50d401f38cded';\n    \n        b=document.getElementsByTagName('body')[0];\n        i1=b.innerHTML.indexOf(t1)+t1.length;\n        i2=b.innerHTML.indexOf(t2);\n \n        code_string = b.innerHTML.substring(i1, i2);\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\n\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \n        \/\/ in the XML parser.\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\n        \/\/ doesn't go ahead and substitute the less-than character. \n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\n\n        copyright = 'Copyright 2016 The MathWorks, Inc.';\n\n        w = window.open();\n        d = w.document;\n        d.write('<pre>\\n');\n        d.write(code_string);\n\n        \/\/ Add copyright line at the bottom if specified.\n        if (copyright.length > 0) {\n            d.writeln('');\n            d.writeln('%%');\n            if (copyright.length > 0) {\n                d.writeln('% _' + copyright + '_');\n            }\n        }\n\n        d.write('<\/pre>\\n');\n\n        d.title = title + ' (MATLAB code)';\n        d.close();\n    }   \n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_750d0863927f46519ce50d401f38cded()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\n      Published with MATLAB&reg; R2016a<br><\/p><p class=\"footer\"><br>\n      Published with MATLAB&reg; R2016a<br><\/p><\/div><!--\n750d0863927f46519ce50d401f38cded ##### SOURCE BEGIN #####\n%%\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\n% Jiro>'s pick this week is a feature that allows you to\n% <https:\/\/www.mathworks.com\/help\/releases\/R2016a\/matlab\/creating_plots\/create-callbacks-for-interacting-with-legend-items.html\n% create interactive legends using callbacks>.\n%\n% This week, I'd like to highlight one of the new\n% <https:\/\/www.mathworks.com\/products\/new_products\/latest_features.html?s_tid=hp_spot_R2016a_0316\n% R2016a features> that just came out a couple of weeks ago. There are so\n% many exciting\n% <https:\/\/www.mathworks.com\/help\/matlab\/release-notes.html#R2016a\n% features>, and the one I'll be talking about today is related to a\n% <https:\/\/blogs.mathworks.com\/pick\/2008\/10\/31\/clickablelegend\/ Pick> from a\n% while ago on <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/21799-clickablelegend-interactive-highlighting-of-data-in-figures\n% |clickableLegend|> by one of our ex-MathWorker,\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/6055857 Ameya>.\n% Ameya's entry allowed you to create an interactive legend for turning on\n% and off particular lines.\n%\n% With R2016a, you can specify custom actions that get executed when one\n% clicks on a legend item. Let's try with the following data set.\n\nx = linspace(0,10);\ny1 = sin(x);\ny2 = cos(x);\ny3 = sin(x) + cos(x);\ny4 = sin(x) .* cos(x);\nplot(x,y1,x,y2,x,y3,x,y4)\n\n%%\n% <<custom_legend_initial_plot.png>>\n%\n% *Toggle Visibility*\n%\n% To use this new feature, you first create a function that defines the\n% specific action you want to perform. For example, the following function\n% toggles the visibility of the line.\n%\n%   function action1(src,event)\n%   % This callback toggles the visibility of the line\n% \n%   if strcmp(event.Peer.Visible,'on')   % If current line is visible\n%       event.Peer.Visible = 'off';      %   Set the visibility to 'off'\n%   else                                 % Else\n%       event.Peer.Visible = 'on';       %   Set the visibility to 'on'\n%   end\n%\n% Refer to the\n% <https:\/\/www.mathworks.com\/help\/releases\/R2016a\/matlab\/ref\/legend-properties.html#property_itemhitfcn\n% |ItemHitFcn|> property for the explanation of the |event| data structure.\n%\n% Then, you assign the function to the |ItemHitFcn| property of the\n% <https:\/\/www.mathworks.com\/help\/releases\/R2016a\/matlab\/ref\/legend.html\n% |legend|> object.\n\nhLeg = legend('Line 1','Line 2','Line 3','Line 4');\nhLeg.ItemHitFcn = @action1;\n\n%%\n% <<custom_legend_1.gif>>\n%\n% *Blinking Line*\n%\n% Here is a different function that blinks the line that is clicked.\n%\n%   function action2(src,event)\n%   % This callback causes the line to \"blink\"\n% \n%   for id = 1:3                        % Repeat 3 times\n%       event.Peer.LineWidth = 3;       % Set line width to 3\n%       pause(0.2)                      % Pause 0.2 seconds\n%       event.Peer.LineWidth = 0.5;     % Set line width to 0.5\n%       pause(0.2)                      % Pause 0.2 seconds\n%   end\n\nhLeg.ItemHitFcn = @action2;\n\n%%\n% <<custom_legend_2.gif>>\n%\n% *Displaying Line Elsewhere*\n%\n% Here is another function that displays the line on a different set of\n% axes.\n%\n%   function action3(src,event,hAx)\n%   % This callback displays the selected line on a different set of axes\n% \n%   x = event.Peer.XData;                   % Get X data of interest\n%   y = event.Peer.YData;                   % Get Y data of interest\n%   plot(hAx,x,y,'Color',event.Peer.Color)  % Plot data with the same color\n%   title(hAx,event.Peer.DisplayName)       % Set the title to the line name\n\nsubplot(2,1,1)\nplot(x,y1,x,y2,x,y3,x,y4)\nhLeg = legend('Line 1','Line 2','Line 3','Line 4');\nhAx = subplot(2,1,2);\nhLeg.ItemHitFcn = @(src,event) action3(src,event,hAx);\n\n%%\n% <<custom_legend_3.gif>>\n%\n% *Comments*\n%\n% Let us know what you think\n% <https:\/\/blogs.mathworks.com\/pick\/?p=6691#respond here>.\n\n##### SOURCE END ##### 750d0863927f46519ce50d401f38cded\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_customlegend\/custom_legend_initial_plot.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\nJiro's pick this week is a feature that allows you to create interactive legends using callbacks.This week, I'd like to highlight one of the new R2016a features that just came out a couple of weeks... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2016\/03\/25\/interactive-legend-in-r2016a\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6691"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=6691"}],"version-history":[{"count":7,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6691\/revisions"}],"predecessor-version":[{"id":6700,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6691\/revisions\/6700"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=6691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=6691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=6691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}