{"id":2443,"date":"2009-07-24T13:24:30","date_gmt":"2009-07-24T13:24:30","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2009\/07\/24\/using-multiple-colormaps-in-a-single-figure\/"},"modified":"2009-07-28T14:38:24","modified_gmt":"2009-07-28T14:38:24","slug":"using-multiple-colormaps-in-a-single-figure","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2009\/07\/24\/using-multiple-colormaps-in-a-single-figure\/","title":{"rendered":"Using multiple colormaps in a single figure"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>Former \"Pickmaster\" (and <a href=\"https:\/\/blogs.mathworks.com\/videos\/\"><i>current<\/i> blogger<\/a>) Doug Hull suggested that <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/18787\">John Iversen<\/a>'s <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/7943-freezecolors---unfreezecolors\">color freezer<\/a> might be \"Pickworthy.\" I wholeheartedly agree.\r\n      <\/p>\r\n   <\/introduction>\r\n   <p>Suppose you wanted to display the image of a clown that ships as a MATLAB demo. This snippet would do the trick:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">load <span style=\"color: #A020F0\">clown<\/span>;\r\nimage(X);\r\ncolormap(map);<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/freezecolors_potw_01.png\"> <p>Similarly, if you wanted to display a penny with a copper colormap, this would work:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">load <span style=\"color: #A020F0\">penny.mat<\/span>\r\ncontour(P,15)\r\ncolormap(copper)\r\naxis <span style=\"color: #A020F0\">ij<\/span> <span style=\"color: #A020F0\">square<\/span>\r\npcolor(P)\r\naxis <span style=\"color: #A020F0\">ij<\/span> <span style=\"color: #A020F0\">square<\/span>\r\nshading <span style=\"color: #A020F0\">flat<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/freezecolors_potw_02.png\"> <p>But what if you wanted to show the two graphics in one figure window? Colormaps are properties of figures, so it's a bit more\r\n      complicated to do that than you might think:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">figure(<span style=\"color: #A020F0\">'color'<\/span>,<span style=\"color: #A020F0\">'w'<\/span>)\r\nsubplot(1,2,1)\r\nimage(X);\r\ncolormap(map);\r\nsubplot(1,2,2)\r\ncontour(P,15)\r\ncolormap(copper)\r\naxis <span style=\"color: #A020F0\">ij<\/span> <span style=\"color: #A020F0\">square<\/span>\r\npcolor(P)\r\naxis <span style=\"color: #A020F0\">ij<\/span> <span style=\"color: #A020F0\">square<\/span>\r\nshading <span style=\"color: #A020F0\">flat<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/freezecolors_potw_03.png\"> <p>Notice that the second call to <tt>colormap<\/tt> affects the image of the clown as well as that of the penny. There are ways to circumvent this behavior. Notably, you could\r\n      <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/creating_plots\/bqsxy8a-1.html#brdjjbz-1\">combine the two colormaps<\/a> into one, and then use different portions of the concatenated colormap for each graphic displayed. Or, if you have the <a href=\"https:\/\/www.mathworks.com\/products\/image\/\">Image Processing Toolbox<\/a> and are dealing exclusively with images, you can use function <tt>subimage<\/tt> to visualize images with different colormaps.\r\n   <\/p>\r\n   <p>But John's submission makes it considerably easier to combine graphics; just issue a <tt>freezeColors<\/tt> command after drawing the clown image, then generate the display of the penny:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">figure(<span style=\"color: #A020F0\">'color'<\/span>,<span style=\"color: #A020F0\">'w'<\/span>)\r\nsubplot(1,2,1)\r\nimage(X);\r\ncolormap(map);\r\n<span style=\"color: #228B22\">% Here's John's contribution:<\/span>\r\nfreezeColors\r\n\r\nsubplot(1,2,2)\r\ncontour(P,15)\r\ncolormap(copper)\r\naxis <span style=\"color: #A020F0\">ij<\/span> <span style=\"color: #A020F0\">square<\/span>\r\npcolor(P)\r\naxis <span style=\"color: #A020F0\">ij<\/span> <span style=\"color: #A020F0\">square<\/span>\r\nshading <span style=\"color: #A020F0\">flat<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/freezecolors_potw_04.png\"> <p>I've been meaning to write a similar function for years. Now I don't have to. Thanks, John!<\/p>\r\n   <p>Comments? Leave them <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2443#respond\"> here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_1308d5c662c74ba3814d8f115a4cfb75() {\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='1308d5c662c74ba3814d8f115a4cfb75 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 1308d5c662c74ba3814d8f115a4cfb75';\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 2009 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_1308d5c662c74ba3814d8f115a4cfb75()\"><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.8<br><\/p>\r\n<\/div>\r\n<!--\r\n1308d5c662c74ba3814d8f115a4cfb75 ##### SOURCE BEGIN #####\r\n%% Using multiple colormaps in a single figure\r\n% Former \"Pickmaster\" (and <https:\/\/blogs.mathworks.com\/videos\/ _current_ blogger>) Doug Hull suggested that <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/18787 John Iversen>'s <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/7943-freezecolors---unfreezecolors color freezer> might be \"Pickworthy.\" I wholeheartedly agree.\r\n\r\n%%\r\n% Suppose you wanted to display the image of a clown that ships\r\n% as a MATLAB demo. This snippet would do the trick:\r\n\r\n%%\r\nload clown;\r\nimage(X);\r\ncolormap(map);\r\n%%\r\n% Similarly, if you wanted to display a penny with a copper\r\n% colormap, this would work:\r\nload penny.mat\r\ncontour(P,15)\r\ncolormap(copper)\r\naxis ij square\r\npcolor(P)\r\naxis ij square\r\nshading flat\r\n%% \r\n% But what if you wanted to show the two graphics in one figure\r\n% window? Colormaps are properties of figures, so it's a bit more complicated to do that than you might think:\r\n\r\nfigure('color','w')\r\nsubplot(1,2,1)\r\nimage(X);\r\ncolormap(map);\r\nsubplot(1,2,2)\r\ncontour(P,15)\r\ncolormap(copper)\r\naxis ij square\r\npcolor(P)\r\naxis ij square\r\nshading flat \r\n%%\r\n% Notice that the second call to |colormap| affects the image of\r\n% the clown as well as that of the penny. There are ways to\r\n% circumvent this behavior. Notably, you could <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/creating_plots\/bqsxy8a-1.html#brdjjbz-1 combine the two colormaps> into one, and then use different portions of the\r\n% concatenated colormap for each graphic displayed. Or, if you\r\n% have the <https:\/\/www.mathworks.com\/products\/image\/ Image Processing Toolbox> and are dealing exclusively\r\n% with images, you can use function |subimage| to visualize\r\n% images with different colormaps.\r\n\r\n%%\r\n% But John's submission makes it considerably easier to combine graphics; just issue a\r\n% |freezeColors| command after drawing the clown image, then\r\n% generate the display of the penny:\r\nfigure('color','w')\r\nsubplot(1,2,1)\r\nimage(X);\r\ncolormap(map);\r\n% Here's John's contribution: \r\nfreezeColors\r\n\r\nsubplot(1,2,2)\r\ncontour(P,15)\r\ncolormap(copper)\r\naxis ij square\r\npcolor(P)\r\naxis ij square\r\nshading flat \r\n\r\n%% \r\n% I've been meaning to write a similar function for years. Now I\r\n% don't have to. Thanks, John!\r\n\r\n%%\r\n% Comments? Leave them <https:\/\/blogs.mathworks.com\/pick\/?p=2443#respond  here>.\r\n\r\n\r\n##### SOURCE END ##### 1308d5c662c74ba3814d8f115a4cfb75\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Former \"Pickmaster\" (and current blogger) Doug Hull suggested that John Iversen's color freezer might be \"Pickworthy.\" I wholeheartedly agree.\r\n      \r\n   \r\n   Suppose you wanted to... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2009\/07\/24\/using-multiple-colormaps-in-a-single-figure\/\">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\/2443"}],"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=2443"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2443\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}