{"id":5563,"date":"2019-11-04T13:09:16","date_gmt":"2019-11-04T18:09:16","guid":{"rendered":"https:\/\/blogs.mathworks.com\/cleve\/?p=5563"},"modified":"2019-11-04T13:09:16","modified_gmt":"2019-11-04T18:09:16","slug":"color-order-for-line-plots","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/cleve\/2019\/11\/04\/color-order-for-line-plots\/","title":{"rendered":"Color Order for Line Plots"},"content":{"rendered":"<div class=\"content\"><!--introduction--><p>Line plots with a color order from one of our color maps are useful, and pretty.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#6367f293-7ad1-4972-a5cf-0605cb8120ec\">Default<\/a><\/li><li><a href=\"#79ab2bff-6b0c-4ef2-b2bd-82153857ffa8\">Parula<\/a><\/li><li><a href=\"#ac830ae1-fdc2-49f5-8484-f7c254439318\">Jet<\/a><\/li><li><a href=\"#863432e4-81ca-4768-abde-720716d12313\">Copper<\/a><\/li><li><a href=\"#fb22d603-b3b9-47d2-80e1-0c9a2ed8dd1d\">Peaks<\/a><\/li><li><a href=\"#b084ee88-43aa-45f9-a21b-c8e0fe91f4f8\">Kuramoto<\/a><\/li><li><a href=\"#2a5eb1c7-c146-42bf-a164-d5a38a99fc04\">Clean up<\/a><\/li><\/ul><\/div><h4>Default<a name=\"6367f293-7ad1-4972-a5cf-0605cb8120ec\"><\/a><\/h4><p>When you plot a two dimensional array, you ordinarily get a bunch of lines, colored like this.<\/p><pre class=\"codeinput\">  n = 19;\r\n  plot(magic(n),<span class=\"string\">'linewidth'<\/span>,2)\r\n  title(sprintf(<span class=\"string\">'magic(%d)'<\/span>,n))\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/cleve\/files\/cplot_blog_01.png\" alt=\"\"> <p>The color of these lines is obtained by cycling through the \"color order\", which, by default, is these seven colors.<\/p><pre class=\"codeinput\">  rgb = get(gca,<span class=\"string\">'colororder'<\/span>)\r\n  show_colors(rgb)\r\n<\/pre><pre class=\"codeoutput\">rgb =\r\n         0    0.4470    0.7410\r\n    0.8500    0.3250    0.0980\r\n    0.9290    0.6940    0.1250\r\n    0.4940    0.1840    0.5560\r\n    0.4660    0.6740    0.1880\r\n    0.3010    0.7450    0.9330\r\n    0.6350    0.0780    0.1840\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/cleve\/files\/cplot_blog_02.png\" alt=\"\"> <p>This default color order is designed to distinguish distinct lines by well separated colors.  It does a good job at this.<\/p><h4>Parula<a name=\"79ab2bff-6b0c-4ef2-b2bd-82153857ffa8\"><\/a><\/h4><p>But I often want to emphasize the interrelations among related lines. So, I set the color order to one obtained from our colormaps. <tt>Parula<\/tt> is my first choice.<\/p><pre class=\"codeinput\">  set(gca,<span class=\"string\">'colororder'<\/span>,parula(7))\r\n  rgb = get(gca,<span class=\"string\">'colororder'<\/span>)\r\n  show_colors(rgb)\r\n<\/pre><pre class=\"codeoutput\">rgb =\r\n    0.2422    0.1504    0.6603\r\n    0.2780    0.3556    0.9777\r\n    0.1540    0.5902    0.9218\r\n    0.0704    0.7457    0.7258\r\n    0.5044    0.7993    0.3480\r\n    0.9871    0.7348    0.2438\r\n    0.9769    0.9839    0.0805\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/cleve\/files\/cplot_blog_03.png\" alt=\"\"> <p>I am about to use this function.<\/p><pre class=\"codeinput\">  type <span class=\"string\">cplot.m<\/span>\r\n<\/pre><pre class=\"codeoutput\">\r\nfunction cplot(Y,cmap)\r\n    close\r\n    [m,n] = size(Y);\r\n    a = axes('colororder',cmap(m));\r\n    line(a,1:n,Y,'linewidth',2)\r\n    box on\r\nend\r\n<\/pre><p>Here is my first example.<\/p><pre class=\"codeinput\">  n = 19;\r\n  cplot(magic(n),@parula)\r\n  title(sprintf(<span class=\"string\">'magic(%d)'<\/span>,n))\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/cleve\/files\/cplot_blog_04.png\" alt=\"\"> <p>By the way, you are seeing three kinds of magic squares -- when the order <tt>n<\/tt> is odd, when <tt>n<\/tt> is divisible by four, and when <tt>n<\/tt> is even but not divisable by four.<\/p><h4>Jet<a name=\"ac830ae1-fdc2-49f5-8484-f7c254439318\"><\/a><\/h4><p>Let's not forget our former best friend, <tt>jet<\/tt>.<\/p><pre class=\"codeinput\">  show_colors(jet(7))\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/cleve\/files\/cplot_blog_05.png\" alt=\"\"> <pre class=\"codeinput\">  n = 20;\r\n  cplot(magic(n),@jet)\r\n  title(sprintf(<span class=\"string\">'magic(%d)'<\/span>,n))\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/cleve\/files\/cplot_blog_06.png\" alt=\"\"> <h4>Copper<a name=\"863432e4-81ca-4768-abde-720716d12313\"><\/a><\/h4><p>I am especially fond of the <tt>copper<\/tt> colormap in these situations.<\/p><pre class=\"codeinput\">  show_colors(copper(7))\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/cleve\/files\/cplot_blog_07.png\" alt=\"\"> <p>Singly even magic squares are the most complicated.<\/p><pre class=\"codeinput\">  n = 18;\r\n  cplot(magic(n),@copper)\r\n  title(sprintf(<span class=\"string\">'magic(%d)'<\/span>,n))\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/cleve\/files\/cplot_blog_08.png\" alt=\"\"> <h4>Peaks<a name=\"fb22d603-b3b9-47d2-80e1-0c9a2ed8dd1d\"><\/a><\/h4><p>We ordinarily use our <tt>peaks<\/tt> function to demo <tt>surf<\/tt> or <tt>contour<\/tt> plots, but it is also useful to view <tt>peaks<\/tt> as a series of lines.<\/p><pre class=\"codeinput\">  n = 40;\r\n  cplot(peaks(n)',@parula)\r\n  title(sprintf(<span class=\"string\">'peaks(%d)'<\/span>,n))\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/cleve\/files\/cplot_blog_09.png\" alt=\"\"> <h4>Kuramoto<a name=\"b084ee88-43aa-45f9-a21b-c8e0fe91f4f8\"><\/a><\/h4><p>I am using colored line plots in my blog posts about <a href=\"https:\/\/blogs.mathworks.com\/cleve\/2019\/10\/30\/stability-of-kuramoto-oscillators\/\">Kuramoto oscillators<\/a>.<\/p><pre class=\"codeinput\">  load <span class=\"string\">history<\/span> <span class=\"string\">H<\/span>\r\n  kuramoto_plots(H)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/cleve\/files\/cplot_blog_10.png\" alt=\"\"> <h4>Clean up<a name=\"2a5eb1c7-c146-42bf-a164-d5a38a99fc04\"><\/a><\/h4><pre class=\"codeinput\">  set(gcf,<span class=\"string\">'position'<\/span>,<span class=\"string\">'factory'<\/span>)\r\n  close\r\n<\/pre><script language=\"JavaScript\"> <!-- \r\n    function grabCode_b7cb9ef04cc34d01aee87531f02cdd17() {\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='b7cb9ef04cc34d01aee87531f02cdd17 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' b7cb9ef04cc34d01aee87531f02cdd17';\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_b7cb9ef04cc34d01aee87531f02cdd17()\"><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><\/div><!--\r\nb7cb9ef04cc34d01aee87531f02cdd17 ##### SOURCE BEGIN #####\r\n%% Color Order for Line Plots\r\n% Line plots with a color order from one of our color maps are useful,\r\n% and pretty.\r\n\r\n%% Default\r\n% When you plot a two dimensional array, you ordinarily get a bunch of\r\n% lines, colored like this.\r\n \r\n  n = 19;\r\n  plot(magic(n),'linewidth',2)\r\n  title(sprintf('magic(%d)',n))\r\n  \r\n%%\r\n% The color of these lines is obtained by cycling through\r\n% the \"color order\", which, by default, is these seven colors.\r\n\r\n  rgb = get(gca,'colororder')\r\n  show_colors(rgb)\r\n  \r\n%%\r\n% This default color order is designed to distinguish distinct lines\r\n% by well separated colors.  It does a good job at this.\r\n\r\n%% Parula\r\n% But I often want to emphasize the interrelations among related lines.\r\n% So, I set the color order to one obtained from our colormaps.\r\n% |Parula| is my first choice.\r\n\r\n  set(gca,'colororder',parula(7))\r\n  rgb = get(gca,'colororder')\r\n  show_colors(rgb)\r\n\r\n%%\r\n% I am about to use this function.\r\n\r\n  type cplot.m\r\n  \r\n%%\r\n% Here is my first example.\r\n\r\n  n = 19;\r\n  cplot(magic(n),@parula)\r\n  title(sprintf('magic(%d)',n))\r\n  \r\n%%\r\n% By the way, you are seeing three kinds of magic squares REPLACE_WITH_DASH_DASH when the order\r\n% |n| is odd, when |n| is divisible by four, and when |n| is even but\r\n% not divisable by four.\r\n\r\n%% Jet\r\n% Let's not forget our former best friend, |jet|.\r\n\r\n  show_colors(jet(7))\r\n\r\n%%\r\n  n = 20;\r\n  cplot(magic(n),@jet)\r\n  title(sprintf('magic(%d)',n))\r\n  \r\n%% Copper\r\n% I am especially fond of the |copper| colormap in these situations.\r\n\r\n  show_colors(copper(7))\r\n  \r\n%% \r\n% Singly even magic squares are the most complicated.\r\n\r\n  n = 18;\r\n  cplot(magic(n),@copper)\r\n  title(sprintf('magic(%d)',n))\r\n  \r\n%% Peaks\r\n% We ordinarily use our |peaks| function to demo |surf| or |contour| plots,\r\n% but it is also useful to view |peaks| as a series of lines.\r\n\r\n  n = 40;\r\n  cplot(peaks(n)',@parula)\r\n  title(sprintf('peaks(%d)',n))\r\n\r\n%% Kuramoto\r\n% I am using colored line plots in my blog posts about \r\n% <https:\/\/blogs.mathworks.com\/cleve\/2019\/10\/30\/stability-of-kuramoto-oscillators\/\r\n% Kuramoto oscillators>.\r\n\r\n  load history H\r\n  kuramoto_plots(H)\r\n  \r\n%% Clean up\r\n\r\n  set(gcf,'position','factory')\r\n  close\r\n  \r\n\r\n##### SOURCE END ##### b7cb9ef04cc34d01aee87531f02cdd17\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/cleve\/files\/cplot_blog_08.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><!--introduction--><p>Line plots with a color order from one of our color maps are useful, and pretty.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/cleve\/2019\/11\/04\/color-order-for-line-plots\/\">read more >><\/a><\/p>","protected":false},"author":78,"featured_media":5583,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[32,23,9],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/5563"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/users\/78"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/comments?post=5563"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/5563\/revisions"}],"predecessor-version":[{"id":5565,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/5563\/revisions\/5565"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/media\/5583"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/media?parent=5563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/categories?post=5563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/tags?post=5563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}