{"id":2869,"date":"2011-06-24T12:51:20","date_gmt":"2011-06-24T12:51:20","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2011\/06\/24\/flexible-legends\/"},"modified":"2011-06-25T15:47:34","modified_gmt":"2011-06-25T15:47:34","slug":"flexible-legends","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2011\/06\/24\/flexible-legends\/","title":{"rendered":"Flexible Legends"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/31092-legendflex-m--a-more-flexible--customizable-legend\"><tt>legendflex<\/tt><\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/17594\">Kelly Kearney<\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <p>When I talk to users who are experienced in <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/creating_plots\/f7-20419.html\">Handle Graphics<\/a>, I sometimes get asked about customizing <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/legend.html\">legends<\/a>. This could be somewhat tricky, especially since legends are axes objects with various callbacks and listeners built in.\r\n      When I ask what exactly they would want to customize, they mention things like adding titles and tiling legends in different\r\n      orientations.\r\n   <\/p>\r\n   <p>Earlier this year, <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911\">Brett<\/a> wrote a <a href=\"https:\/\/blogs.mathworks.com\/pick\/2011\/02\/11\/create-multi-column-plot-legends\/\">post<\/a> highlighting <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27389-simonhenin-columnlegend\"><tt>columnlegend<\/tt><\/a> which allows you to create legends with multiple columns. Kelly's <tt>legendflex<\/tt> takes it one step further and gives you additional flexibility in adding titles and placing multiple legends systematically.\r\n   <\/p>\r\n   <p>Take a look at this example where I show solutions to a 2nd order ordinary differential equation with varying parameters.\r\n      The line color and style represent different things, and the multiple legends accurately describe that.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Define parameters<\/span>\r\nm = 5;         <span style=\"color: #228B22\">% mass<\/span>\r\nb = [0.5 1 2]; <span style=\"color: #228B22\">% damping<\/span>\r\nk = [0.5 1 2]; <span style=\"color: #228B22\">% stiffness<\/span>\r\n\r\n<span style=\"color: #228B22\">% Define colors and styles<\/span>\r\ncolors     = {[1 0 0], [0 .5 0], [0 0 1]};\r\nlinestyles = {<span style=\"color: #A020F0\">'-'<\/span>, <span style=\"color: #A020F0\">'--'<\/span>, <span style=\"color: #A020F0\">':'<\/span>};\r\n\r\n<span style=\"color: #228B22\">% Preallocate variable<\/span>\r\nh = nan(length(k), length(b));\r\n\r\n<span style=\"color: #228B22\">% Simulate for each combination of b and k<\/span>\r\n<span style=\"color: #0000FF\">for<\/span> kID = 1:length(k)\r\n   <span style=\"color: #0000FF\">for<\/span> bID = 1:length(b)\r\n      [T, Y] = ode45(@(t, y) [y(2); -1\/m*(k(kID)*y(1)+b(bID)*y(2))], <span style=\"color: #0000FF\">...<\/span>\r\n         [0, 25], <span style=\"color: #0000FF\">...<\/span><span style=\"color: #228B22\">   % simulate for 25 seconds<\/span>\r\n         [1, 0]);       <span style=\"color: #228B22\">% initial conditions<\/span>\r\n\r\n      <span style=\"color: #228B22\">% Plot<\/span>\r\n      h(kID, bID) = line(T, Y(:, 1), <span style=\"color: #0000FF\">...<\/span>\r\n         <span style=\"color: #A020F0\">'LineWidth'<\/span>, 2, <span style=\"color: #0000FF\">...<\/span>\r\n         <span style=\"color: #A020F0\">'Color'<\/span>    , colors{kID}, <span style=\"color: #0000FF\">...<\/span>\r\n         <span style=\"color: #A020F0\">'LineStyle'<\/span>, linestyles{bID});\r\n   <span style=\"color: #0000FF\">end<\/span>\r\n<span style=\"color: #0000FF\">end<\/span>\r\n\r\nylim([-1.2 1.2]); box <span style=\"color: #A020F0\">on<\/span>;\r\nxlabel(<span style=\"color: #A020F0\">'Time (sec)'<\/span>);\r\nylabel(<span style=\"color: #A020F0\">'Displacement (m)'<\/span>);\r\ntitle(sprintf([<span style=\"color: #A020F0\">'5x\\\\prime\\\\prime + b x\\\\prime + k x = 0\\n'<\/span>, <span style=\"color: #0000FF\">...<\/span>\r\n   <span style=\"color: #A020F0\">'x = 1, x\\\\prime = 0'<\/span>]));\r\n\r\n<span style=\"color: #228B22\">% Create a legend for Damping and place it flush against the corner<\/span>\r\n[h1, o_h] = legendflex(h(1, :), cellstr(num2str(b')), <span style=\"color: #0000FF\">...<\/span>\r\n   <span style=\"color: #A020F0\">'ref'<\/span>, gca, <span style=\"color: #A020F0\">'anchor'<\/span>, {<span style=\"color: #A020F0\">'ne'<\/span>, <span style=\"color: #A020F0\">'ne'<\/span>}, <span style=\"color: #A020F0\">'buffer'<\/span>, [0 0], <span style=\"color: #0000FF\">...<\/span>\r\n   <span style=\"color: #A020F0\">'title'<\/span>, {<span style=\"color: #A020F0\">'Line Style'<\/span>, <span style=\"color: #A020F0\">'Damping (N-s\/m)'<\/span>});\r\n<span style=\"color: #228B22\">% Set the color of lines to black<\/span>\r\nset(o_h, <span style=\"color: #A020F0\">'Color'<\/span>, <span style=\"color: #A020F0\">'black'<\/span>);\r\n\r\n<span style=\"color: #228B22\">% Create a legend for Stiffness and place it 10 pixels left of<\/span>\r\n<span style=\"color: #228B22\">%        the Damping legend<\/span>\r\nlegendflex(h(:, 1), cellstr(num2str(k')), <span style=\"color: #0000FF\">...<\/span>\r\n   <span style=\"color: #A020F0\">'ref'<\/span>, h1, <span style=\"color: #A020F0\">'anchor'<\/span>, {<span style=\"color: #A020F0\">'nw'<\/span>, <span style=\"color: #A020F0\">'ne'<\/span>}, <span style=\"color: #A020F0\">'buffer'<\/span>, [-10 0], <span style=\"color: #0000FF\">...<\/span>\r\n   <span style=\"color: #A020F0\">'title'<\/span>, {<span style=\"color: #A020F0\">'Color'<\/span>, <span style=\"color: #A020F0\">'Stiffness (N\/m)'<\/span>});<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_legendflex\/potw_legendflex_01.png\"> <p>Another thing to note is the well-documented code. Kelly has included extensive help with an example. I'm also happy to see\r\n      the use of <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/inputparser.html\"><tt>inputParser<\/tt><\/a>, <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/validateattributes.html\"><tt>validateattributes<\/tt><\/a>, and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/validatestring.html\"><tt>validatestring<\/tt><\/a> which are very powerful argument validation tools.\r\n   <\/p>\r\n   <p><b>Comments<\/b><\/p>\r\n   <p>Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2869#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/31092-legendflex-m--a-more-flexible--customizable-legend#comments\">comment<\/a> for Kelly.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_b1e314ee2cc64e63b867aefa8047364c() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='b1e314ee2cc64e63b867aefa8047364c ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' b1e314ee2cc64e63b867aefa8047364c';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Jiro Doke';\r\n        copyright = 'Copyright 2011 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_b1e314ee2cc64e63b867aefa8047364c()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.12<br><\/p>\r\n<\/div>\r\n<!--\r\nb1e314ee2cc64e63b867aefa8047364c ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/31092-legendflex-m--a-more-flexible--customizable-legend |legendflex|>\r\n% by <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/17594\r\n% Kelly Kearney>.\r\n%\r\n\r\n%%\r\n% When I talk to users who are experienced in\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/creating_plots\/f7-20419.html\r\n% Handle Graphics>, I sometimes get asked about customizing\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/legend.html legends>. This\r\n% could be somewhat tricky, especially since legends are axes objects with\r\n% various callbacks and listeners built in. When I ask what exactly they\r\n% would want to customize, they mention things like adding titles and\r\n% tiling legends in different orientations.\r\n%\r\n% Earlier this year,\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911 Brett>\r\n% wrote a\r\n% <https:\/\/blogs.mathworks.com\/pick\/2011\/02\/11\/create-multi-column-plot-legends\/\r\n% post> highlighting\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27389-simonhenin-columnlegend\r\n% |columnlegend|> which allows you to create legends with multiple columns.\r\n% Kelly's |legendflex| takes it one step further and gives you additional\r\n% flexibility in adding titles and placing multiple legends systematically.\r\n%\r\n% Take a look at this example where I show solutions to a 2nd order\r\n% ordinary differential equation with varying parameters. The line color\r\n% and style represent different things, and the multiple legends accurately\r\n% describe that.\r\n\r\n% Define parameters\r\nm = 5;         % mass\r\nb = [0.5 1 2]; % damping\r\nk = [0.5 1 2]; % stiffness\r\n\r\n% Define colors and styles\r\ncolors     = {[1 0 0], [0 .5 0], [0 0 1]};\r\nlinestyles = {'-', 'REPLACE_WITH_DASH_DASH', ':'};\r\n\r\n% Preallocate variable\r\nh = nan(length(k), length(b));\r\n\r\n% Simulate for each combination of b and k\r\nfor kID = 1:length(k)\r\n   for bID = 1:length(b)\r\n      [T, Y] = ode45(@(t, y) [y(2); -1\/m*(k(kID)*y(1)+b(bID)*y(2))], ...\r\n         [0, 25], ...   % simulate for 25 seconds\r\n         [1, 0]);       % initial conditions\r\n      \r\n      % Plot\r\n      h(kID, bID) = line(T, Y(:, 1), ...\r\n         'LineWidth', 2, ...\r\n         'Color'    , colors{kID}, ...\r\n         'LineStyle', linestyles{bID});\r\n   end\r\nend\r\n\r\nylim([-1.2 1.2]); box on;\r\nxlabel('Time (sec)');\r\nylabel('Displacement (m)');\r\ntitle(sprintf(['5x\\\\prime\\\\prime + b x\\\\prime + k x = 0\\n', ...\r\n   'x = 1, x\\\\prime = 0']));\r\n\r\n% Create a legend for Damping and place it flush against the corner\r\n[h1, o_h] = legendflex(h(1, :), cellstr(num2str(b')), ...\r\n   'ref', gca, 'anchor', {'ne', 'ne'}, 'buffer', [0 0], ...\r\n   'title', {'Line Style', 'Damping (N-s\/m)'});\r\n% Set the color of lines to black\r\nset(o_h, 'Color', 'black');\r\n\r\n% Create a legend for Stiffness and place it 10 pixels left of\r\n%        the Damping legend\r\nlegendflex(h(:, 1), cellstr(num2str(k')), ...\r\n   'ref', h1, 'anchor', {'nw', 'ne'}, 'buffer', [-10 0], ...\r\n   'title', {'Color', 'Stiffness (N\/m)'});\r\n      \r\n%%\r\n% Another thing to note is the well-documented code. Kelly has included\r\n% extensive help with an example. I'm also happy to see the use of\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/inputparser.html\r\n% |inputParser|>,\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/validateattributes.html\r\n% |validateattributes|>, and\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/validatestring.html\r\n% |validatestring|> which are very powerful argument validation tools.\r\n%\r\n% *Comments*\r\n%\r\n% Give it a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=2869#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/31092-legendflex-m--a-more-flexible--customizable-legend#comments\r\n% comment> for Kelly.\r\n\r\n##### SOURCE END ##### b1e314ee2cc64e63b867aefa8047364c\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Jiro's pick this week is legendflex by Kelly Kearney.\r\n      \r\n   \r\n   When I talk to users who are experienced in Handle Graphics, I sometimes get asked about customizing legends. This... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2011\/06\/24\/flexible-legends\/\">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\/2869"}],"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=2869"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2869\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}