{"id":4160,"date":"2020-08-18T09:06:30","date_gmt":"2020-08-18T13:06:30","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=4160"},"modified":"2020-08-18T09:14:11","modified_gmt":"2020-08-18T13:14:11","slug":"making-color-spectrum-plots-part-3","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2020\/08\/18\/making-color-spectrum-plots-part-3\/","title":{"rendered":"Making Color Spectrum Plots &#8211; Part 3"},"content":{"rendered":"<div class=\"content\"><p>Recently, I've been explaining how I made this plot, which is from <a href=\"http:\/\/www.imageprocessingplace.com\/DIPUM-3E\/dipum3e_main_page.htm\"><b>DIPUM3E<\/b><\/a> (<i>Digital Image Processing Using MATLAB<\/i>, 3rd ed.):<\/p><p><img decoding=\"async\" style=\"width:200px;\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/illuminant-d65.png\" alt=\"\"> <\/p><p>In my <a href=\"https:\/\/blogs.mathworks.com\/steve\/2020\/07\/20\/making-color-spectrum-plots-part-2\/\">July 20 post<\/a>, I showed one way to compute the spectral colors to display below the x-axis. Today I'll finish up by explaining the use of the <tt>colorbar<\/tt> function. These techniques are used in the <a href=\"http:\/\/www.imageprocessingplace.com\/DIPUM-3E\/dipum3e_main_page.htm\"><b>DIPUM3E<\/b><\/a> functions <tt>spectrumBar<\/tt> and <tt>spectrumColors<\/tt>, which are available to you in <i>MATLAB Color Tools<\/i> on the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/64161-matlab-color-tools\">File Exchange<\/a> and on <a href=\"https:\/\/github.com\/mathworks\/matlab-color-tools\">GitHub<\/a>. The entire set of <a href=\"http:\/\/www.imageprocessingplace.com\/DIPUM-3E\/dipum3e_main_page.htm\"><b>DIPUM3E<\/b><\/a> functions is also on <a href=\"https:\/\/github.com\/dipum\/dipum-toolbox\">GitHub<\/a>.<\/p><p>Let's just start with a plot and a colorbar with the default settings.<\/p><pre class=\"codeinput\">T = illuminant(<span class=\"string\">\"D65\"<\/span>);\r\nplot(T.lambda,T.S)\r\nxlabel(<span class=\"string\">'wavelength (nm)'<\/span>)\r\nylabel(<span class=\"string\">'Relative spectral power'<\/span>)\r\ntitle(<span class=\"string\">'Illuminant D65'<\/span>)\r\ngrid <span class=\"string\">on<\/span>\r\nxlim([380 780])\r\ncb = colorbar\r\n<\/pre><pre class=\"codeoutput\">\r\ncb = \r\n\r\n  ColorBar with properties:\r\n\r\n    Location: 'eastoutside'\r\n      Limits: [0 1]\r\n    FontSize: 9\r\n    Position: [0.8482 0.1095 0.0286 0.8167]\r\n       Units: 'normalized'\r\n\r\n  Use GET to show all properties\r\n\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/steve\/files\/using_colorbar_01.png\" alt=\"\"> <p>Well, let's fix the colors first. The colorbar automatically picks up its colors from the axes <tt>Colormap<\/tt> property, if it is set, or from the figure <tt>Colormap<\/tt> property. I'll set it on the axes. I also need to set the <tt>CLim<\/tt> property to establish the relationship between the colormap colors and the spectrum wavelength values.<\/p><pre class=\"codeinput\">[colors,lambda] = spectrumColors;\r\nax = gca;\r\nax.Colormap = colors;\r\nax.CLim = [min(lambda) max(lambda)];\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/steve\/files\/using_colorbar_02.png\" alt=\"\"> <p>Now let's get it in the right place.<\/p><pre class=\"codeinput\">cb.Location = <span class=\"string\">'southoutside'<\/span>;\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/steve\/files\/using_colorbar_03.png\" alt=\"\"> <p>The final tweaks set things up so that the x-axis labels appear under the colorbar.<\/p><pre class=\"codeinput\">cb.Ticks = ax.XTick;\r\ncb.Label.String = ax.XLabel.String;\r\ncb.TickDirection = <span class=\"string\">\"out\"<\/span>;\r\nax.XTickLabels = [];\r\nax.XLabel = [];\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/steve\/files\/using_colorbar_04.png\" alt=\"\"> <p>There you go. This nontraditional use of <tt>colorbar<\/tt> works pretty well as an auxiliary color label for the x-axis (or y-axis). If you find another use for this technique, let us know in the comments.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_401f25e6e1f74be7b333c14b8ed64091() {\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='401f25e6e1f74be7b333c14b8ed64091 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 401f25e6e1f74be7b333c14b8ed64091';\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 2020 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_401f25e6e1f74be7b333c14b8ed64091()\"><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; R2020a<br><\/p><\/div><!--\r\n401f25e6e1f74be7b333c14b8ed64091 ##### SOURCE BEGIN #####\r\n%% \r\n% Recently, I've been explaining how I made this plot, which is from\r\n% <http:\/\/www.imageprocessingplace.com\/DIPUM-3E\/dipum3e_main_page.htm\r\n% *DIPUM3E*> (_Digital Image Processing Using MATLAB_, 3rd ed.):\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/steve\/files\/illuminant-d65.png>>\r\n%\r\n% In my\r\n% <https:\/\/blogs.mathworks.com\/steve\/2020\/07\/20\/making-color-spectrum-plots-part-2\/\r\n% July 20 post>, I showed one way to compute the spectral colors to\r\n% display below the x-axis. Today I'll finish up by explaining the use\r\n% of the |colorbar| function. These techniques are used in the\r\n% <http:\/\/www.imageprocessingplace.com\/DIPUM-3E\/dipum3e_main_page.htm\r\n% *DIPUM3E*> functions |spectrumBar| and |spectrumColors|, which are\r\n% available to you in _MATLAB Color Tools_ on the\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/64161-matlab-color-tools\r\n% File Exchange> and on <https:\/\/github.com\/mathworks\/matlab-color-tools\r\n% GitHub>. The entire set of\r\n% <http:\/\/www.imageprocessingplace.com\/DIPUM-3E\/dipum3e_main_page.htm\r\n% *DIPUM3E*> functions is also on\r\n% <https:\/\/github.com\/dipum\/dipum-toolbox GitHub>.\r\n%\r\n% Let's just start with a plot and a colorbar with the default settings.\r\n\r\nT = illuminant(\"D65\");\r\nplot(T.lambda,T.S)\r\nxlabel('wavelength (nm)')\r\nylabel('Relative spectral power')\r\ntitle('Illuminant D65')\r\ngrid on\r\nxlim([380 780])\r\ncb = colorbar\r\n\r\n%%\r\n% Well, let's fix the colors first. The colorbar automatically picks up\r\n% its colors from the axes |Colormap| property, if it is set, or from the\r\n% figure |Colormap| property. I'll set it on the axes. I also need to\r\n% set the |CLim| property to establish the relationship between the\r\n% colormap colors and the spectrum wavelength values.\r\n\r\n[colors,lambda] = spectrumColors;\r\nax = gca;\r\nax.Colormap = colors;\r\nax.CLim = [min(lambda) max(lambda)];\r\n\r\n%%\r\n% Now let's get it in the right place.\r\ncb.Location = 'southoutside';\r\n\r\n%%\r\n% The final tweaks set things up so that the x-axis labels appear\r\n% under the colorbar.\r\n\r\ncb.Ticks = ax.XTick;\r\ncb.Label.String = ax.XLabel.String;\r\ncb.TickDirection = \"out\";\r\nax.XTickLabels = [];\r\nax.XLabel = [];\r\n\r\n%%\r\n% There you go. This nontraditional use of |colorbar| works pretty well\r\n% as an auxiliary color label for the x-axis (or y-axis). If you find\r\n% another use for this technique, let us know in the comments.\r\n##### SOURCE END ##### 401f25e6e1f74be7b333c14b8ed64091\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/steve\/files\/using_colorbar_04.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><p>Recently, I've been explaining how I made this plot, which is from DIPUM3E (Digital Image Processing Using MATLAB, 3rd ed.): In my July 20 post, I showed one way to compute the spectral colors to... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2020\/08\/18\/making-color-spectrum-plots-part-3\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":4164,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[1103,80,70,122,380,68,52,94,360,96],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/4160"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/comments?post=4160"}],"version-history":[{"count":4,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/4160\/revisions"}],"predecessor-version":[{"id":4176,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/4160\/revisions\/4176"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media\/4164"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=4160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=4160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=4160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}