{"id":9346,"date":"2018-01-12T09:59:07","date_gmt":"2018-01-12T14:59:07","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=9346"},"modified":"2018-01-12T09:59:07","modified_gmt":"2018-01-12T14:59:07","slug":"non-uniform-contourf","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2018\/01\/12\/non-uniform-contourf\/","title":{"rendered":"Non-Uniform Contourf"},"content":{"rendered":"\r\n<div class=\"content\"><p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871\">Jiro<\/a>&#8216;s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/65424-non-uniform-contourf-imagesc-colorbar\">non-uniform contourf\/imagesc\/colorbar<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2155584\">Yuxin Jiang<\/a>.<\/p><p>I discovered this File Exchange entry after seeing a <a href=\"https:\/\/jp.mathworks.com\/matlabcentral\/answers\/374370\">question<\/a> (note, the question is in Japanese) on MATLAB Answers. The user wanted to create a <tt>contourf<\/tt> with a particular color at a given range. There are probably a number of ways to achieve this, including the answer provided by Akira.<\/p><p>This entry by Yuxin gives the flexibility to specify non-uniformly spaced levels, as well as custom colormaps. Let&#8217;s take a look.<\/p><pre class=\"codeinput\">data = peaks;\r\n<\/pre><p>First, a normal filled contour plot.<\/p><pre class=\"codeinput\">contourf(data)\r\ncolorbar\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_contourfnu\/potw_contourfnu_01.png\" alt=\"\"> <p>Next, let&#8217;s create a contour plot where the values between -1 and 1 are gray.<\/p><pre class=\"codeinput\">edges = -10:10;        <span class=\"comment\">% create 20 levels<\/span>\r\ncmap = parula(20);     <span class=\"comment\">% create colormap<\/span>\r\ncmap(10:11,:) = .5;    <span class=\"comment\">% set the color corresponding to -1~1 to gray<\/span>\r\n<\/pre><p>Then we call <tt>contourfnu<\/tt>.<\/p><pre class=\"codeinput\">contourfnu(1:49,1:49,data,edges,cmap,[],[],<span class=\"string\">'contourf'<\/span>)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_contourfnu\/potw_contourfnu_02.png\" alt=\"\"> <p><b>Comments<\/b><\/p><p>Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=9346#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/65424-non-uniform-contourf-imagesc-colorbar#comment\">comment<\/a> for Yuxin.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_67383a3d061649e2a31ac75400624bd2() {\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='67383a3d061649e2a31ac75400624bd2 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 67383a3d061649e2a31ac75400624bd2';\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 2018 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_67383a3d061649e2a31ac75400624bd2()\"><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; R2017b<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2017b<br><\/p><\/div><!--\r\n67383a3d061649e2a31ac75400624bd2 ##### SOURCE BEGIN #####\r\n%% \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871 Jiro>'s \r\n% pick this week is <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/65424-non-uniform-contourf-imagesc-colorbar \r\n% non-uniform contourf\/imagesc\/colorbar> by <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2155584 \r\n% Yuxin Jiang>.\r\n% \r\n% I discovered this File Exchange entry after seeing a <https:\/\/jp.mathworks.com\/matlabcentral\/answers\/374370 \r\n% question> (note, the question is in Japanese) on MATLAB Answers. The user wanted \r\n% to create a |contourf| with a particular color at a given range. There are probably \r\n% a number of ways to achieve this, including the answer provided by Akira.\r\n% \r\n% This entry by Yuxin gives the flexibility to specify non-uniformly spaced \r\n% levels, as well as custom colormaps. Let's take a look.\r\n\r\ndata = peaks;\r\n%% \r\n% First, a normal filled contour plot.\r\n\r\ncontourf(data)\r\ncolorbar\r\n%% \r\n% Next, let's create a contour plot where the values between -1 and 1 are \r\n% gray.\r\n%%\r\nedges = -10:10;        % create 20 levels\r\ncmap = parula(20);     % create colormap\r\ncmap(10:11,:) = .5;    % set the color corresponding to -1~1 to gray\r\n%% \r\n% Then we call |contourfnu|.\r\n\r\ncontourfnu(1:49,1:49,data,edges,cmap,[],[],'contourf')\r\n%% \r\n% *Comments*\r\n% \r\n% Give it a try and let us know what you think <https:\/\/blogs.mathworks.com\/pick\/?p=9346#respond \r\n% here> or leave a <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/65424-non-uniform-contourf-imagesc-colorbar#comment \r\n% comment> for Yuxin.\r\n##### SOURCE END ##### 67383a3d061649e2a31ac75400624bd2\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_contourfnu\/potw_contourfnu_01.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\nJiro&#8216;s pick this week is non-uniform contourf\/imagesc\/colorbar by Yuxin Jiang.I discovered this File Exchange entry after seeing a question (note, the question is in Japanese) on MATLAB&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2018\/01\/12\/non-uniform-contourf\/\">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\/9346"}],"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=9346"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/9346\/revisions"}],"predecessor-version":[{"id":9350,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/9346\/revisions\/9350"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=9346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=9346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=9346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}