{"id":2631,"date":"2010-12-23T19:54:50","date_gmt":"2010-12-23T19:54:50","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2010\/12\/23\/generate-distinct-colors-for-your-matlab-plots\/"},"modified":"2010-12-23T19:54:50","modified_gmt":"2010-12-23T19:54:50","slug":"generate-distinct-colors-for-your-matlab-plots","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2010\/12\/23\/generate-distinct-colors-for-your-matlab-plots\/","title":{"rendered":"Generate Distinct Colors for Your MATLAB Plots"},"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\/29702-generate-maximally-perceptually-distinct-colors\">\"Generate Maximally Perceptually-Distinct Colors,\"<\/a>, by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/38323\">Tim Holy<\/a>.\r\n   <\/p>\r\n   <p>Today, I'm going out on a limb: I'm going to Pick a new file from a new File Exchange author. As of this writing, \"Generate\r\n      Maximally-Perceptually Distinct Colors\" (GMPDC)--the author's first File Exchange submission (welcome!)--has been downloaded\r\n      only three times. (My download was the third.) However, I was excited to see this file, and I know it will come in handy.\r\n      In fact, I've often considered writing something like this myself--but have never gotten around to it.\r\n   <\/p>\r\n   <p>So what does GMPDC do? It provides an easy way to generate distinct, differentiable colors in which to plot a long sequence\r\n      of objects (like lines).\r\n   <\/p>\r\n   <p>MATLAB ships with several built-in colormaps that are useful in different situations. One of those, \"lines,\" is a repeating\r\n      array of seven distinct colors. By default, MATLAB axes cycle through these seven different colors when plotting lines:\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/..\/images\/pick\/defaultcolororder.png\"> <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">%<\/span>\r\n<span style=\"color: #228B22\">% You can see the default color order with:<\/span>\r\n\r\nget(0,<span style=\"color: #A020F0\">'DefaultAxesColorOrder'<\/span>)\r\n\r\n<span style=\"color: #228B22\">% and you can verify that they are the same as the repeating sequence in<\/span>\r\n<span style=\"color: #228B22\">% \"lines\" with<\/span>\r\n\r\nisequal(lines(7),get(0,<span style=\"color: #A020F0\">'DefaultAxesColorOrder'<\/span>))<\/pre><pre style=\"font-style:oblique\">ans =\r\n         0         0    1.0000\r\n    1.0000         0         0\r\n         0    1.0000         0\r\n         0         0    0.1724\r\n    1.0000    0.1034    0.7241\r\n    1.0000    0.8276         0\r\n         0    0.3448         0\r\nans =\r\n     1\r\n<\/pre><p>Sometimes, however, I want to be able to differentiate more than 7 lines or plots. In the past, I've monkeyed about with custom\r\n      color orders I made up on the fly. Tim makes it easy to standardize those colors; the colors returned by GMPDC are consistent\r\n      regardless of the number of colors you request. As a bonus, one can specify a background color as an optional input to ensure\r\n      that your colors differ from the background, as well as from each other:\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/..\/images\/pick\/gmpdc.png\"> <\/p>\r\n   <p>Tim worked in the L*a*b* colorspace, which was designed to approximate human color perception. His code is solid and well\r\n      implemented, with nice documentation. I hope this file is a harbinger of more good stuff to come from this author.\r\n   <\/p>\r\n   <p>Note that the default \"colororder\" property can be readily modified. In fact, I just added a line to my <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/startup.html\">startup file<\/a> to set my colororder to be <tt>distinguishable_colors(20)<\/tt>. Thanks, Tim!\r\n   <\/p>\r\n   <p>Try GMPDC for yourself; you'll be modifying your own startup files before you know it! <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2631#respond\">let us know what you think<\/a>, or leave a comment for Tim <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/29702-generate-maximally-perceptually-distinct-colors#Comments\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_cd872ffb143747e78621a0738875fad3() {\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='cd872ffb143747e78621a0738875fad3 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' cd872ffb143747e78621a0738875fad3';\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 2010 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_cd872ffb143747e78621a0738875fad3()\"><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\ncd872ffb143747e78621a0738875fad3 ##### SOURCE BEGIN #####\r\n%% Generate Distinct Colors\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\/29702-generate-maximally-perceptually-distinct-colors \"Generate Maximally Perceptually-Distinct Colors,\">, by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/38323 Tim Holy>.\r\n\r\n%%\r\n% Today, I'm going out on a limb: I'm going to Pick a new file from a new\r\n% File Exchange author. As of this writing, \"Generate\r\n% Maximally-Perceptually Distinct Colors\" (GMPDC)REPLACE_WITH_DASH_DASHthe author's first File\r\n% Exchange submission (welcome!)REPLACE_WITH_DASH_DASHhas been downloaded only three times. (My\r\n% download was the third.) However, I was excited to see this file, and I\r\n% know it will come in handy. In fact, I've often considered writing\r\n% something like this myselfREPLACE_WITH_DASH_DASHbut have never gotten around to it.\r\n\r\n%%\r\n% So what does GMPDC do? It provides an easy way to generate distinct,\r\n% differentiable colors in which to plot a long sequence of objects (like\r\n% lines).\r\n\r\n%%\r\n% MATLAB ships with several built-in colormaps that are useful in different\r\n% situations. One of those, \"lines,\" is a repeating array of seven distinct colors.\r\n% By default, MATLAB axes cycle through these seven different colors when\r\n% plotting lines:\r\n\r\n%%\r\n% \r\n% <<..\/DefaultColorOrder.png>>\r\n% \r\n\r\n%\r\n% You can see the default color order with:\r\n\r\nget(0,'DefaultAxesColorOrder')\r\n\r\n% and you can verify that they are the same as the repeating sequence in\r\n% \"lines\" with\r\n\r\nisequal(lines(7),get(0,'DefaultAxesColorOrder'))\r\n\r\n%%\r\n% Sometimes, however, I want to be able to differentiate more than 7 lines\r\n% or plots. In the past, I've monkeyed about with custom color orders I\r\n% made up on the fly. Tim makes it easy to standardize those colors; the\r\n% colors returned by GMPDC are consistent regardless of the number of\r\n% colors you request. As a bonus, one can specify a background color as an\r\n% optional input to ensure that your colors differ from the background, as\r\n% well as from each other:\r\n\r\n%%\r\n% \r\n% <<..\/GMPDC.png>>\r\n% \r\n\r\n\r\n%% \r\n% Tim worked in the L*a*b* colorspace, which was designed to approximate\r\n% human color perception. His code is solid and well implemented, with nice\r\n% documentation. I hope this file is a harbinger of more good stuff to come from this author.\r\n%%\r\n% Note that the default \"colororder\" property can be readily modified. In\r\n% fact, I just added a line to my\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/startup.html startup file>\r\n% to set my colororder to be |distinguishable_colors(20)|. Thanks, Tim!\r\n\r\n%%\r\n% Try GMPDC for yourself; you'll be modifying your own startup files before you know it!\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=2631#respond let us know what you think>, or leave a\r\n% comment for Tim <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/29702-generate-maximally-perceptually-distinct-colors#Comments here>.\r\n\r\n##### SOURCE END ##### cd872ffb143747e78621a0738875fad3\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n   Brett's Pick this week is \"Generate Maximally Perceptually-Distinct Colors,\", by Tim Holy.\r\n   \r\n   Today, I'm going out on a limb: I'm going to Pick a new file from a new File Exchange... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2010\/12\/23\/generate-distinct-colors-for-your-matlab-plots\/\">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\/2631"}],"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=2631"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2631\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}