{"id":1765,"date":"2016-04-25T12:12:01","date_gmt":"2016-04-25T16:12:01","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=1765"},"modified":"2019-11-01T16:38:19","modified_gmt":"2019-11-01T20:38:19","slug":"clim-caxis-imshow-and-imagesc","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2016\/04\/25\/clim-caxis-imshow-and-imagesc\/","title":{"rendered":"CLim, caxis, imshow, and imagesc"},"content":{"rendered":"<div class=\"content\"><p>In response to <a href=\"https:\/\/blogs.mathworks.com\/steve\/2016\/03\/21\/matlab-image-display-autoscaling-values-with-imshow\/\">\"MATLAB image display - autoscaling values with imshow,\"<\/a> MATLAB Answerer Extraordinaire <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1343420-image-analyst\">ImageAnalyst<\/a> posted this comment:<\/p><p><i>A discussion of the relationship and interplay of caxis(), CLim, and the values you can pass in inside the brackets to imshow() or imagesc() might be useful. For example, let's say your values range from 200 to 35,000, and you want all values less than 1000 to be blue and all values more than 29000 to be red. And you want a colorbar with 16 steps - 16 discrete colors. How would one go about that, and using which functions?<\/i><\/p><p>Good question! Let's have a go at it, starting with <tt>CLim<\/tt>.<\/p><p><tt>CLim<\/tt> is a property of an <a title=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/axes-properties.html (link no longer works)\"><tt>Axes<\/tt><\/a> object.<\/p><p>To investigate <tt>CLim<\/tt>, start with <tt>imagesc<\/tt>, some elevation data, and a color bar.<\/p><pre class=\"codeinput\">load <span class=\"string\">mt_monadnock.mat<\/span>\r\n\r\nimagesc(Zc)\r\naxis <span class=\"string\">image<\/span>\r\n\r\ncolorbar\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/clim_caxis_imshow_01.jpg\" alt=\"\"> <p>The <tt>Axes<\/tt> object controls many aspects of the plot, including the axes rulers, the ticks, the tick labels, the grid lines, and much more. The function <tt>gca<\/tt> (\"get current axes\") returns the <tt>Axes<\/tt> object.<\/p><pre class=\"codeinput\">ax = gca\r\n<\/pre><pre class=\"codeoutput\">\r\nax = \r\n\r\n  Axes with properties:\r\n\r\n             XLim: [0.5000 3.0425e+03]\r\n             YLim: [0.5000 3.0425e+03]\r\n           XScale: 'linear'\r\n           YScale: 'linear'\r\n    GridLineStyle: '-'\r\n         Position: [0.1168 0.1100 0.6965 0.8150]\r\n            Units: 'normalized'\r\n\r\n  Use GET to show all properties\r\n\r\n<\/pre><p>The <tt>Axes<\/tt> object has a large number of properties, so by default MATLAB shows you just the most commonly used ones. If you run this code interactively, you would see a clickable \"Show all properties\" link.<\/p><p>One of those properties is <tt>CLim<\/tt> (\"color limits\"), which you can access directly this way:<\/p><pre class=\"codeinput\">ax.CLim\r\n<\/pre><pre class=\"codeoutput\">\r\nans =\r\n\r\n  141.5285  963.1366\r\n\r\n<\/pre><p>If you look closely at the color bar in the image plot above, you can see the correspondence between it and the <tt>CLim<\/tt> values. <tt>ax.CLim(1)<\/tt> is the bottom value on the color bar, and <tt>ax.Clim(2)<\/tt> is the top value.<\/p><p>Where did those values come from, though?<\/p><p>They were automatically computed from the range of the data being plotted.<\/p><pre class=\"codeinput\">min(Zc(:))\r\n<\/pre><pre class=\"codeoutput\">\r\nans =\r\n\r\n  141.5285\r\n\r\n<\/pre><pre class=\"codeinput\">max(Zc(:))\r\n<\/pre><pre class=\"codeoutput\">\r\nans =\r\n\r\n  963.1366\r\n\r\n<\/pre><p>You can set the <tt>CLim<\/tt> yourself, though, and that changes the way the color is scaled from the data values. Let's set the color limits to expand the visible details of the lower elevations.<\/p><pre class=\"codeinput\">ax.CLim = [140 400];\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/clim_caxis_imshow_02.jpg\" alt=\"\"> <p>Or maybe you want to examine the upper elevations.<\/p><pre class=\"codeinput\">ax.CLim = [600 970];\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/clim_caxis_imshow_03.jpg\" alt=\"\"> <p>ImageAnalyst mentioned the function <tt>caxis<\/tt>. That's just a convenient way to set the color limits. It's one step shorter than getting the <tt>Axes<\/tt> using <tt>gca<\/tt> and then setting its <tt>CLim<\/tt> property.<\/p><pre class=\"codeinput\">caxis([400 600])\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/clim_caxis_imshow_04.jpg\" alt=\"\"> <p>You can also use <tt>caxis<\/tt> to quickly get back to automatic computation of color limits.<\/p><pre class=\"codeinput\">caxis(<span class=\"string\">'auto'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/clim_caxis_imshow_05.jpg\" alt=\"\"> <p>Then ImageAnalyst asked about the [low high] syntax for <tt>imagesc<\/tt> and <tt>imshow<\/tt>. This is just another convenience for setting the color limits.<\/p><pre class=\"codeinput\">imagesc(Zc,[400 600])\r\naxis <span class=\"string\">image<\/span>\r\ncolorbar\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/clim_caxis_imshow_06.jpg\" alt=\"\"> <pre class=\"codeinput\">ax = gca;\r\nax.CLim\r\n<\/pre><pre class=\"codeoutput\">\r\nans =\r\n\r\n   400   600\r\n\r\n<\/pre><p>The final part of ImageAnalyst's comment concerned the number of colors. What if you only want 16 colors? Well, all of the MATLAB colormap functions take an optional input argument specifying the number of colors to use. So just call the colormap function that you want to use and pass it the desired number of colors.<\/p><pre class=\"codeinput\">caxis(<span class=\"string\">'auto'<\/span>)\r\ncolormap(parula(16))\r\ntitle(<span class=\"string\">'Full range, 16 colors'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/clim_caxis_imshow_07.jpg\" alt=\"\"> <script language=\"JavaScript\"> <!-- \r\n    function grabCode_35430028b727476987445ca21909da11() {\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='35430028b727476987445ca21909da11 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 35430028b727476987445ca21909da11';\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 2016 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_35430028b727476987445ca21909da11()\"><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; R2016a<br><\/p><\/div><!--\r\n35430028b727476987445ca21909da11 ##### SOURCE BEGIN #####\r\n%%\r\n% In response to <https:\/\/blogs.mathworks.com\/steve\/2016\/03\/21\/matlab-image-display-autoscaling-values-with-imshow\/\r\n% \"MATLAB image display - autoscaling values with imshow,\">\r\n% MATLAB Answerer Extraordinaire <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1343420-image-analyst \r\n% ImageAnalyst> posted this comment:\r\n%\r\n% _A discussion of the relationship and interplay of caxis(), CLim, and the\r\n% values you can pass in inside the brackets to imshow() or imagesc() might\r\n% be useful. For example, let?s say your values range from 200 to 35,000,\r\n% and you want all values less than 1000 to be blue and all values more than\r\n% 29000 to be red. And you want a colorbar with 16 steps - 16 discrete\r\n% colors. How would one go about that, and using which functions?_\r\n%\r\n% Good question! Let's have a go at it, starting with |CLim|.\r\n%\r\n% |CLim| is a property of an <https:\/\/www.mathworks.com\/help\/matlab\/ref\/axes-properties.html \r\n% |Axes|> object.\r\n%\r\n% To investigate |CLim|, start with |imagesc|, some elevation data, and a\r\n% color bar.\r\n\r\nload mt_monadnock.mat\r\n\r\nimagesc(Zc)\r\naxis image\r\n\r\ncolorbar\r\n\r\n%%\r\n% The |Axes| object controls many aspects of the plot, including the axes\r\n% rulers, the ticks, the tick labels, the grid lines, and much more. The\r\n% function |gca| (\"get current axes\") returns the |Axes| object.\r\n\r\nax = gca\r\n\r\n%%\r\n% The |Axes| object has a large number of properties, so by default MATLAB\r\n% shows you just the most commonly used ones. If you run this code\r\n% interactively, you would see a clickable \"Show all properties\" link.\r\n%\r\n% One of those properties is |CLim| (\"color limits\"), which you can access\r\n% directly this way:\r\n\r\nax.CLim\r\n \r\n%%\r\n% If you look closely at the color bar in the image plot above, you can\r\n% see the correspondence between it and the |CLim| values. |ax.CLim(1)| is\r\n% the bottom value on the color bar, and |ax.Clim(2)| is the top value.\r\n%\r\n% Where did those values come from, though?\r\n%\r\n% They were automatically computed from the range of the data being plotted.\r\n\r\nmin(Zc(:))\r\n\r\n%%\r\n\r\nmax(Zc(:))\r\n\r\n%%\r\n% You can set the |CLim| yourself, though, and that changes the way the\r\n% color is scaled from the data values. Let's set the color limits to expand\r\n% the visible details of the lower elevations.\r\n\r\nax.CLim = [140 400];\r\n\r\n%%\r\n% Or maybe you want to examine the upper elevations.\r\n\r\nax.CLim = [600 970];\r\n\r\n%%\r\n% ImageAnalyst mentioned the function |caxis|. That's just a convenient way\r\n% to set the color limits. It's one step shorter than getting the |Axes|\r\n% using |gca| and then setting its |CLim| property.\r\n\r\ncaxis([400 600])\r\n\r\n%%\r\n% You can also use |caxis| to quickly get back to automatic computation of\r\n% color limits.\r\n\r\ncaxis('auto')\r\n\r\n%%\r\n% Then ImageAnalyst asked about the [low high] syntax for |imagesc| and\r\n% |imshow|. This is just another convenience for setting the color limits.\r\n\r\nimagesc(Zc,[400 600])\r\naxis image\r\ncolorbar\r\n\r\n%%\r\nax = gca;\r\nax.CLim\r\n\r\n%%\r\n% The final part of ImageAnalyst's comment concerned the number of colors.\r\n% What if you only want 16 colors? Well, all of the MATLAB colormap\r\n% functions take an optional input argument specifying the number of colors\r\n% to use. So just call the colormap function that you want to use and pass\r\n% it the desired number of colors.\r\n\r\ncaxis('auto')\r\ncolormap(parula(16))\r\ntitle('Full range, 16 colors')\r\n##### SOURCE END ##### 35430028b727476987445ca21909da11\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/steve\/files\/clim_caxis_imshow_02.jpg\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><p>In response to \"MATLAB image display - autoscaling values with imshow,\" MATLAB Answerer Extraordinaire ImageAnalyst posted this comment:A discussion of the relationship and interplay of caxis(),... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2016\/04\/25\/clim-caxis-imshow-and-imagesc\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":1768,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[595,50,1153,1103,58,60,362,122,380,1093,52],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1765"}],"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=1765"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1765\/revisions"}],"predecessor-version":[{"id":1780,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1765\/revisions\/1780"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media\/1768"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=1765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=1765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=1765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}