{"id":2291,"date":"2008-08-15T08:49:51","date_gmt":"2008-08-15T13:49:51","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2008\/08\/15\/colors-for-your-multi-line-plots\/"},"modified":"2016-05-17T14:52:22","modified_gmt":"2016-05-17T18:52:22","slug":"colors-for-your-multi-line-plots","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2008\/08\/15\/colors-for-your-multi-line-plots\/","title":{"rendered":"Colors for Your Multi-Line Plots"},"content":{"rendered":"<div class=\"content\">\r\n\r\nJiro's pick this week is <a title=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadFile.do?objectId=21050&amp;objectType=file (link no longer works)\">VARYCOLOR<\/a> by Daniel Helmick.\r\n\r\n&nbsp;\r\n\r\nHave you ever had to plot many lines on a single graph and you had to construct additional set of colors to augment the 8\r\nbuilt-in colors? You may have to come up with a good set of colors that span a wide range of the spectrum. Daniel's <tt>varycolor<\/tt> will give you that color set with a single command.\r\n\r\nLet's say I'm going to plot 50 lines. I can get the color matrix like this:\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">ColorSet = varycolor(50);<\/pre>\r\nI can either index into this color matrix and use it as my <tt>Color<\/tt> property for <tt>plot<\/tt>:\r\n<pre>    plot(rand(10,1), 'Color', ColorSet(1,:))<\/pre>\r\nOr set it as the new <tt>ColorOrder<\/tt> property of an axes. Now, my subsequent plots will follow the new color order. (Be sure to use <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/hold.html\">hold all<\/a><\/tt> to preserve the color order.)\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">set(gca, <span style=\"color: #a020f0;\">'ColorOrder'<\/span>, ColorSet);\r\n\r\nhold <span style=\"color: #a020f0;\">all<\/span>;\r\n<span style=\"color: #0000ff;\">for<\/span> m = 1:50\r\n  plot([0 51-m], [0 m]);\r\n<span style=\"color: #0000ff;\">end<\/span><\/pre>\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/multilinecolors_01.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/>\r\n\r\nWhen I have this many lines, I often wonder what the best way to put a legend is. I can simply add the default legend:\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">legend <span style=\"color: #a020f0;\">show<\/span> <span style=\"color: #a020f0;\">Location<\/span> <span style=\"color: #a020f0;\">NorthEastOutside<\/span><\/pre>\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/multilinecolors_02.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/>\r\n\r\nBut this is quite impractical. Instead, I can use the color matrix that I just created and use that in my color bar.\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">legend <span style=\"color: #a020f0;\">off<\/span>\r\nset(gcf, <span style=\"color: #a020f0;\">'Colormap'<\/span>, ColorSet);\r\ncolorbar<\/pre>\r\n<img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/multilinecolors_03.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/>\r\n\r\nSince I appropriately chose the number of colors to be the same as the number of lines, the colormap scale corresponds to\r\nthe line number. Line 1 is green and line 40 is red.\r\n\r\nAs a bonus, take a look at Loren's blog on <a href=\"https:\/\/blogs.mathworks.com\/loren\/2007\/12\/19\/plotting-with-style\/\">Plotting with Style<\/a> to learn about other ways to customize the line style.\r\n\r\n<b>Comments<\/b>\r\n\r\nTell us <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2291#respond\">here<\/a> how you customize your visualizations to convey your ideas as clearly as possible. Do you use colors, line styles, or markers?\r\nOr maybe some combinations.\r\n\r\n<script>\/\/ <![CDATA[\r\nfunction grabCode_727d782a404d4b108a8a37f0c774de39() {\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='727d782a404d4b108a8a37f0c774de39 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 727d782a404d4b108a8a37f0c774de39';\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 2008 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('\r\n\r\n<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>\r\n\r\n\r\n\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }\r\n\/\/ ]]><\/script>\r\n<p style=\"text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;\">\r\n<a><span style=\"font-size: x-small; font-style: italic;\">Get\r\nthe MATLAB code\r\n<noscript>(requires JavaScript)<\/noscript><\/span><\/a>\r\n\r\nPublished with MATLAB\u00ae 7.6<\/p>\r\n\r\n<\/div>\r\n<!--\r\n727d782a404d4b108a8a37f0c774de39 ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadAuthor.do?objectId=1094142&objectType=author % Jiro>'s pick this week is <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadFile.do?objectId=21050&objectType=file % VARYCOLOR> by <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadAuthor.do?objectType=author&objectId=1111152 % Daniel Helmick>.\r\n\r\n%%\r\n% Have you ever had to plot many lines on a single graph and you had to\r\n% construct additional set of colors to augment the 8 built-in colors? You\r\n% may have to come up with a good set of colors that span a wide range of\r\n% the spectrum. Daniel's |varycolor| will give you that color set with a\r\n% single command.\r\n%\r\n% Let's say I'm going to plot 50 lines. I can get the color matrix like\r\n% this:\r\n\r\nColorSet = varycolor(50);\r\n\r\n%%\r\n% I can either index into this color matrix and use it as my |Color|\r\n% property for |plot|:\r\n%\r\n%      plot(rand(10,1), 'Color', ColorSet(1,:))\r\n%\r\n% Or set it as the new |ColorOrder| property of an axes. Now, my subsequent\r\n% plots will follow the new color order. (Be sure to use\r\n% |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/hold.html % hold all>| to preserve the color order.)\r\n\r\nset(gca, 'ColorOrder', ColorSet);\r\n\r\nhold all;\r\nfor m = 1:50\r\nplot([0 51-m], [0 m]);\r\nend\r\n\r\n%%\r\n% When I have this many lines, I often wonder what the best way to put a\r\n% legend is. I can simply add the default legend:\r\n\r\nlegend show Location NorthEastOutside\r\n\r\n%%\r\n% But this is quite impractical. Instead, I can use the color matrix that I\r\n% just created and use that in my color bar.\r\n\r\nlegend off\r\nset(gcf, 'Colormap', ColorSet);\r\ncolorbar\r\n\r\n%%\r\n% Since I appropriately chose the number of colors to be the same as the\r\n% number of lines, the colormap scale corresponds to the line number. Line\r\n% 1 is green and line 40 is red.\r\n%\r\n% As a bonus, take a look at Loren's blog on\r\n% <https:\/\/blogs.mathworks.com\/loren\/2007\/12\/19\/plotting-with-style\/ % Plotting with Style> to learn about other ways to customize the line\r\n% style.\r\n%\r\n% *Comments*\r\n%\r\n% Tell us <https:\/\/blogs.mathworks.com\/pick\/?p=2291#respond here> how you\r\n% customize your visualizations to convey your ideas as clearly as\r\n% possible. Do you use colors, line styles, or markers? Or maybe some\r\n% combinations.\r\n##### SOURCE END ##### 727d782a404d4b108a8a37f0c774de39\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n\r\nJiro's pick this week is VARYCOLOR by Daniel Helmick.\r\n\r\n&nbsp;\r\n\r\nHave you ever had to plot many lines on a single graph and you had to construct additional set of colors to augment the... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2008\/08\/15\/colors-for-your-multi-line-plots\/\">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\/2291"}],"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=2291"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2291\/revisions"}],"predecessor-version":[{"id":7210,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2291\/revisions\/7210"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}