{"id":2645,"date":"2011-02-11T14:13:48","date_gmt":"2011-02-11T14:13:48","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2011\/02\/11\/create-multi-column-plot-legends\/"},"modified":"2019-03-21T12:09:19","modified_gmt":"2019-03-21T16:09:19","slug":"create-multi-column-plot-legends","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2011\/02\/11\/create-multi-column-plot-legends\/","title":{"rendered":"Create Multi-Column Plot Legends"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction><\/introduction>\r\n   <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911\">Brett<\/a>'s Pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27389-simonhenin-columnlegend\">\"columnlegend,\"<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/83564\">Simon Henin<\/a>.\r\n   <\/p>\r\n<p>\r\n<strong>NOTE<\/strong>: Functionality similar to this File Exchange entry was added to MATLAB in R2018a. See the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/legend.html#d117e768980\">NumColumns Property<\/a> to legend.\r\n<\/p>\r\n\r\n   <p>Today's Pick is another that is an author's first submission to the File Exchange. Simon shared a nice, easy-to-use function\r\n      to create multi-column figure labels.\r\n   <\/p>\r\n   <p>MATLAB's built-in <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/legend.html\"><tt>legend<\/tt><\/a> command suffices for most cases when there are few objects to differentiate. However, when the number of lines plotted, for\r\n      instance, gets large, <tt>columnlegend<\/tt> shines. It enables one essentially to reshape the default n-by-one legend into a p-by-q one.\r\n   <\/p>\r\n   <p>Consider:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">nLines = 18;\r\nlegend_str = cell(nLines,1);\r\n\r\nmyColors = distinguishable_colors(nLines);<\/pre><p>(See my <a href=\"https:\/\/blogs.mathworks.com\/pick\/2010\/12\/23\/generate-distinct-colors-for-your-matlab-plots\/\">previous post about distinguishable_colors<\/a>, or comment out the above line, and uncomment the following one.)\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">%myColors = jet(nLines);<\/span>\r\nt = 0:pi\/64:3*pi;\r\ndPhi = pi\/16;\r\nlineStyles = {<span style=\"color: #A020F0\">'-'<\/span>, <span style=\"color: #A020F0\">'--'<\/span>, <span style=\"color: #A020F0\">':'<\/span>, <span style=\"color: #A020F0\">'-.'<\/span>};\r\n<span style=\"color: #0000FF\">for<\/span> ii=1:nLines,\r\n    plot(t,sin(t+dPhi*ii),<span style=\"color: #0000FF\">...<\/span>\r\n        <span style=\"color: #A020F0\">'linestyle'<\/span>, lineStyles{rem(ii-1,numel(lineStyles))+1},<span style=\"color: #0000FF\">...<\/span>\r\n        <span style=\"color: #A020F0\">'color'<\/span>, myColors(ii,:),<span style=\"color: #0000FF\">...<\/span>\r\n        <span style=\"color: #A020F0\">'linewidth'<\/span>,3);\r\n    hold <span style=\"color: #A020F0\">on<\/span>;\r\n    legend_str{ii} = num2str(ii);\r\n<span style=\"color: #0000FF\">end<\/span>\r\naxis([0 3*pi -1.15 1.6])\r\nlegend(legend_str,<span style=\"color: #A020F0\">'location'<\/span>,<span style=\"color: #A020F0\">'NorthWest'<\/span>)<\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/..\/images\/pick\/legendpic.png\"> <\/p>\r\n   <p>Now consider how <tt>columnlegend<\/tt> improves the situation:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">for<\/span> ii=1:nLines,\r\n    plot(t,sin(t+dPhi*ii),<span style=\"color: #0000FF\">...<\/span>\r\n        <span style=\"color: #A020F0\">'linestyle'<\/span>, lineStyles{rem(ii-1,numel(lineStyles))+1},<span style=\"color: #0000FF\">...<\/span>\r\n        <span style=\"color: #A020F0\">'color'<\/span>, myColors(ii,:),<span style=\"color: #0000FF\">...<\/span>\r\n        <span style=\"color: #A020F0\">'linewidth'<\/span>,3);\r\n    hold <span style=\"color: #A020F0\">on<\/span>;\r\n    legend_str{ii} = num2str(ii);\r\n<span style=\"color: #0000FF\">end<\/span>\r\naxis([0 3*pi -1.15 1.6])\r\ncolumnlegend(6,legend_str,<span style=\"color: #A020F0\">'NorthWest'<\/span>);<\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/..\/images\/pick\/columnlegendpic.png\"> <\/p>\r\n   <p>That's better! <tt>columnlegend<\/tt> also conveniently provides a handle to the legend-containing axes, allowing you to tweak parameters (like position). Very\r\n      nice first effort, Simon!\r\n   <\/p>\r\n   <p>One further note: in a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27389-simonhenin-columnlegend#Comments\">comment<\/a> on his submission page, Simon mentioned that he had fixed an issue with plot markers. As of this writing, that modification\r\n      is still pending; it should be available shortly.\r\n   <\/p>\r\n   <p><a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2645#respond\">Let us know what you think<\/a>, or leave a comment for Simon <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27389-simonhenin-columnlegend#Comments\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_0763bdd87f73480eafb06206f63462f8() {\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='0763bdd87f73480eafb06206f63462f8 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 0763bdd87f73480eafb06206f63462f8';\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 = 'Brett Shoelson';\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_0763bdd87f73480eafb06206f63462f8()\"><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.11<br><\/p>\r\n<\/div>\r\n<!--\r\n0763bdd87f73480eafb06206f63462f8 ##### SOURCE BEGIN #####\r\n%% Create Multi-Column Plot Legends \r\n%% \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911 Brett>'s Pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27389-simonhenin-columnlegend \"columnlegend,\"> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/83564 Simon Henin>.\r\n\r\n%%\r\n% Today's Pick is another that is an author's first submission to the File\r\n% Exchange. Simon shared a nice, easy-to-use function to create\r\n% multi-column figure labels.\r\n\r\n%%\r\n% MATLAB's built-in\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/legend.html |legend|> command \r\n% suffices for most cases when there are few objects to differentiate.\r\n% However, when the number of lines plotted, for instance, gets large,\r\n% |columnlegend| shines. It enables one essentially to reshape the default\r\n% n-by-one legend into a p-by-q one.\r\n\r\n%%\r\n% Consider:\r\n\r\nnLines = 18;\r\nlegend_str = cell(nLines,1);\r\n\r\nmyColors = distinguishable_colors(nLines);\r\n%%\r\n% (See my <https:\/\/blogs.mathworks.com\/pick\/2010\/12\/23\/generate-distinct-colors-for-your-matlab-plots\/ previous post about distinguishable_colors>, or comment out the\r\n% above line, and uncomment the following one.)\r\n%%\r\n%myColors = jet(nLines);\r\nt = 0:pi\/64:3*pi;\r\ndPhi = pi\/16;\r\nlineStyles = {'-', 'REPLACE_WITH_DASH_DASH', ':', '-.'};\r\nfor ii=1:nLines,\r\n    plot(t,sin(t+dPhi*ii),...\r\n        'linestyle', lineStyles{rem(ii-1,numel(lineStyles))+1},...\r\n        'color', myColors(ii,:),...\r\n        'linewidth',3);\r\n    hold on;\r\n    legend_str{ii} = num2str(ii);\r\nend\r\naxis([0 3*pi -1.15 1.6])\r\nlegend(legend_str,'location','NorthWest')\r\n%%\r\n% \r\n% <<https:\/\/blogs.mathworks.com\/pick\/..\/images\/pick\/legendpic.png>>\r\n% \r\n\r\n%%\r\n% Now consider how |columnlegend| improves the situation:\r\nfor ii=1:nLines,\r\n    plot(t,sin(t+dPhi*ii),...\r\n        'linestyle', lineStyles{rem(ii-1,numel(lineStyles))+1},...\r\n        'color', myColors(ii,:),...\r\n        'linewidth',3);\r\n    hold on;\r\n    legend_str{ii} = num2str(ii);\r\nend\r\naxis([0 3*pi -1.15 1.6])\r\ncolumnlegend(6,legend_str,'NorthWest');\r\n%%\r\n% \r\n% <<https:\/\/blogs.mathworks.com\/pick\/..\/images\/pick\/columnlegendpic.png>>\r\n% \r\n\r\n%%\r\n% That's better! |columnlegend| also conveniently provides a handle to the\r\n% legend-containing axes, allowing you to tweak parameters (like\r\n% position). Very nice first effort, Simon!\r\n\r\n%%\r\n% One further note: in a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27389-simonhenin-columnlegend#Comments comment> \r\n% on his submission page, Simon mentioned that he had fixed an issue with\r\n% plot markers. As of this writing, that modification is still pending; it\r\n% should be available shortly.\r\n\r\n%%\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=2645#respond Let us know what you think>, or leave a\r\n% comment for Simon <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27389-simonhenin-columnlegend#Comments here>.\r\n\r\n##### SOURCE END ##### 0763bdd87f73480eafb06206f63462f8\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n   Brett's Pick this week is \"columnlegend,\" by Simon Henin.\r\n   \r\n\r\nNOTE: Functionality similar to this File Exchange entry was added to MATLAB in R2018a. See the NumColumns Property to... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2011\/02\/11\/create-multi-column-plot-legends\/\">read more >><\/a><\/p>","protected":false},"author":34,"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\/2645"}],"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\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=2645"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2645\/revisions"}],"predecessor-version":[{"id":10620,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2645\/revisions\/10620"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}