{"id":10602,"date":"2019-03-22T09:00:58","date_gmt":"2019-03-22T13:00:58","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=10602"},"modified":"2019-03-21T12:03:39","modified_gmt":"2019-03-21T16:03:39","slug":"multi-column-legend-revisited","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2019\/03\/22\/multi-column-legend-revisited\/","title":{"rendered":"Multi-column legend, revisited"},"content":{"rendered":"\r\n\r\n<div class=\"content\"><p><a href=\"http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871\">Jiro<\/a>&#8216;s Pick this week is the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/legend.html#d117e688280\"><tt>NumColumns<\/tt><\/a> property of the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/legend.html\"><tt>legend<\/tt><\/a> function, new as of R2018a.<\/p><p>This week, R2019a was released. This is another exciting release with many new features and capabilities. Read and learn about them <a href=\"https:\/\/www.mathworks.com\/products\/new_products\/latest_features.html\">here<\/a> (note that this page will always show the latest features from the latest release, so depending on when you read this, you may get a page for other than R2019a).<\/p><p>So why am I talking about a feature from R2018a? There are two reasons.<\/p><div><ol><li>The <tt>NumColumns<\/tt> property of <tt>legend<\/tt> allows you to create a multi-column legend, which happened to be a topic of a previous Pick of the Week post, <a href=\"https:\/\/blogs.mathworks.com\/pick\/2011\/02\/11\/create-multi-column-plot-legends\/\">&#8220;Create Multi-Column Plot Legends&#8221;<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/845693\">Brett<\/a>. <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27389\"><tt>columnlegend<\/tt><\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2192205\">Simon<\/a> is still a good entry to have in your utility belt if you are using R2017b or older, but if you have newer releases, you can now do that with the <tt>legend<\/tt> function.<\/li><li>We were looking at some web traffic analytics, and we found out that the Pick of the Week post I mentioned above kept getting a lot of traffic even after 8 years. That meant that there were probably a lot of people wanting to create multi-column legends, and they were landing on the blog post. For those people, I wanted to make sure that they knew about the new feature, in addition to the File Exchange entry.<\/li><\/ol><\/div><p>So, let&#8217;s just see the new legend functionality in action.<\/p><pre class=\"codeinput\">t = 0:pi\/64:3*pi;\r\ndPhi = pi\/16;\r\nset(gca,<span class=\"string\">'LineStyleOrder'<\/span>,{<span class=\"string\">'-'<\/span>,<span class=\"string\">'--'<\/span>,<span class=\"string\">':'<\/span>,<span class=\"string\">'-.'<\/span>})\r\nhold <span class=\"string\">on<\/span>\r\n<span class=\"keyword\">for<\/span> ii = 1:18\r\n    plot(t,sin(t+dPhi*ii))\r\n    legend_str{ii} = num2str(ii);\r\n<span class=\"keyword\">end<\/span>\r\nylim([-1.15 1.6])\r\n\r\nlegend(legend_str,<span class=\"string\">'Location'<\/span>,<span class=\"string\">'NorthWest'<\/span>,<span class=\"string\">'NumColumns'<\/span>,6)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_multiplecolumnlegend_r2018a\/potw_multiplecolumnlegend_r2018a_01.png\" alt=\"\"> <p><b>Comments<\/b><\/p><p>Give it a try and let us know what you think <a href=\"http:\/\/blogs.mathworks.com\/pick\/?p=10602#respond\">here<\/a>. And, you can probably expect future posts highlighting some new R2019a features.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_718f1567f10d44a6bdf81320ce74f427() {\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='718f1567f10d44a6bdf81320ce74f427 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 718f1567f10d44a6bdf81320ce74f427';\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        copyright = 'Copyright 2019 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 copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\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     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_718f1567f10d44a6bdf81320ce74f427()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2018b<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2018b<br><\/p><\/div><!--\r\n718f1567f10d44a6bdf81320ce74f427 ##### SOURCE BEGIN #####\r\n%%\r\n% <http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871 Jiro>'s\r\n% Pick this week is the\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/legend.html#d117e688280\r\n% |NumColumns|> property of the\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/legend.html |legend|>\r\n% function, new as of R2018a.\r\n%\r\n% This week, R2019a was released. This is another exciting release with\r\n% many new features and capabilities. Read and learn about them\r\n% <https:\/\/www.mathworks.com\/products\/new_products\/latest_features.html\r\n% here> (note that this page will always show the latest features from the\r\n% latest release, so depending on when you read this, you may get a page\r\n% for other than R2019a).\r\n%\r\n% So why am I talking about a feature from R2018a? There are two reasons.\r\n%\r\n% # The |NumColumns| property of |legend| allows you to create a\r\n% multi-column legend, which happened to be a topic of a previous Pick of\r\n% the Week post,\r\n% <https:\/\/blogs.mathworks.com\/pick\/2011\/02\/11\/create-multi-column-plot-legends\/\r\n% \"Create Multi-Column Plot Legends\"> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/845693 Brett>.\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27389\r\n% |columnlegend|> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2192205 Simon>\r\n% is still a good entry to have in your utility belt if you are using\r\n% R2017b or older, but if you have newer releases, you can now do that with\r\n% the |legend| function.\r\n% # We were looking at some web traffic analytics, and we found out that\r\n% the Pick of the Week post I mentioned above kept getting a lot of traffic\r\n% even after 8 years. That meant that there were probably a lot of people\r\n% wanting to create multi-column legends, and they were landing on the blog\r\n% post. For those people, I wanted to make sure that they knew about the\r\n% new feature, in addition to the File Exchange entry.\r\n%\r\n% So, let's just see the new legend functionality in action.\r\n\r\nt = 0:pi\/64:3*pi;\r\ndPhi = pi\/16;\r\nset(gca,'LineStyleOrder',{'-','REPLACE_WITH_DASH_DASH',':','-.'})\r\nhold on\r\nfor ii = 1:18\r\n    plot(t,sin(t+dPhi*ii))\r\n    legend_str{ii} = num2str(ii);\r\nend\r\nylim([-1.15 1.6])\r\n\r\nlegend(legend_str,'Location','NorthWest','NumColumns',6)\r\n\r\n%%\r\n%\r\n% *Comments*\r\n%\r\n% Give it a try and let us know what you think\r\n% <http:\/\/blogs.mathworks.com\/pick\/?p=10602#respond here>. And, you can\r\n% probably expect future posts highlighting some new R2019a features.\r\n\r\n##### SOURCE END ##### 718f1567f10d44a6bdf81320ce74f427\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_multiplecolumnlegend_r2018a\/potw_multiplecolumnlegend_r2018a_01.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n\r\nJiro&#8216;s Pick this week is the NumColumns property of the legend function, new as of R2018a.This week, R2019a was released. This is another exciting release with many new features and&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2019\/03\/22\/multi-column-legend-revisited\/\">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\/10602"}],"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=10602"}],"version-history":[{"count":5,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/10602\/revisions"}],"predecessor-version":[{"id":10614,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/10602\/revisions\/10614"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=10602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=10602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=10602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}