{"id":8505,"date":"2017-04-07T09:00:50","date_gmt":"2017-04-07T13:00:50","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=8505"},"modified":"2017-08-27T20:06:15","modified_gmt":"2017-08-28T00:06:15","slug":"r2017a-heatmap","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2017\/04\/07\/r2017a-heatmap\/","title":{"rendered":"R2017a: heatmap"},"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 the new <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/heatmap.html\"><tt>heatmap<\/tt><\/a> function introduced in R2017a.<\/p><p>Last month, a new release of MATLAB (R2017a) came out with many <a href=\"https:\/\/www.mathworks.com\/help\/relnotes\/index.html\">new features<\/a>. Of those, I&#8217;d like to highlight <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/heatmap.html\"><tt>heatmap<\/tt><\/a>, which over the years had <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/?term=heatmap&amp;sort=relevancy\">predecessors<\/a> created by the user community.<\/p><p><b>Meet <tt>heatmap<\/tt><\/b><\/p><p>Heatmap charts display data as an image where each element is represented by a color. The value of the element is also displayed on the image. As a simple example, let&#8217;s visualize a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Magic_square\">magic square<\/a>.<\/p><pre class=\"codeinput\">m = magic(15);\r\nheatmap(m);\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_r2017a_heatmap\/potw_r2017a_heatmap_01.png\" alt=\"\"> <p>You can imagine that if the size of the matrix is large, or if the figure is resized to a small window, the image can quickly become crowded with a lot of text. No need to worry. The heatmap intelligently turns off the text display when it makes sense to do so.<\/p><pre class=\"codeinput\">set(gcf,<span class=\"string\">'Position'<\/span>,[100 100 400 350])\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_r2017a_heatmap\/potw_r2017a_heatmap_02.png\" alt=\"\"> <p><tt>heatmap<\/tt> returns a <tt>HeatmapChart<\/tt> object, which you can use to customize.<\/p><pre class=\"codeinput\">figure\r\nhMap = heatmap(m);\r\nhMap.ColorbarVisible = <span class=\"string\">'off'<\/span>;\r\nhMap.CellLabelColor = <span class=\"string\">'red'<\/span>;\r\nhMap.GridVisible = <span class=\"string\">'off'<\/span>;\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_r2017a_heatmap\/potw_r2017a_heatmap_03.png\" alt=\"\"> <p>Another feature of <tt>heatmap<\/tt> that I like is the ability to pass a <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/tables.html\">table<\/a> data. For tables, you specify the x and the y variables, and the function will calculate and display the aggregated result, such as the count, of the various x-y combinations.<\/p><p>For example, if we have a table of data for 100 people with information about whether they smoke and their self-assessed health status, we can group them into the various categories and visualize as a heatmap.<\/p><pre class=\"codeinput\">load <span class=\"string\">patients<\/span>\r\ntbl = table(Age,Gender,SelfAssessedHealthStatus,Smoker,Weight,Location);\r\nheatmap(tbl,<span class=\"string\">'Smoker'<\/span>,<span class=\"string\">'SelfAssessedHealthStatus'<\/span>);\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_r2017a_heatmap\/potw_r2017a_heatmap_04.png\" alt=\"\"> <p>Instead of showing the aggregated count, we can choose to display the average age for each category.<\/p><pre class=\"codeinput\">heatmap(tbl,<span class=\"string\">'Smoker'<\/span>,<span class=\"string\">'SelfAssessedHealthStatus'<\/span>, <span class=\"keyword\">...<\/span>\r\n    <span class=\"string\">'ColorVariable'<\/span>,<span class=\"string\">'Age'<\/span>);\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_r2017a_heatmap\/potw_r2017a_heatmap_05.png\" alt=\"\"> <p>Or the median weight.<\/p><pre class=\"codeinput\">heatmap(tbl,<span class=\"string\">'Smoker'<\/span>,<span class=\"string\">'SelfAssessedHealthStatus'<\/span>, <span class=\"keyword\">...<\/span>\r\n    <span class=\"string\">'ColorVariable'<\/span>,<span class=\"string\">'Weight'<\/span>,<span class=\"string\">'ColorMethod'<\/span>,<span class=\"string\">'median'<\/span>);\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_r2017a_heatmap\/potw_r2017a_heatmap_06.png\" alt=\"\"> <p><b>For Previous Releases<\/b><\/p><p>If you haven&#8217;t upgraded or cannot upgrade to R2017a, you can check out some of the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/?term=heatmap&amp;sort=relevancy\">user-submitted entries<\/a>. One of my favorites is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24253-customizable-heat-maps\">Customizable Heat Maps<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/6055857\">Ameya<\/a>, a former collegue of mine.<\/p><p><b>Comments<\/b><\/p><p>If you have R2017a, give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=8505#respond\">here<\/a>!<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_a9f73f878f40454a83ceb6bc083488b0() {\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='a9f73f878f40454a83ceb6bc083488b0 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' a9f73f878f40454a83ceb6bc083488b0';\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 2017 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_a9f73f878f40454a83ceb6bc083488b0()\"><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; R2017a<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2017a<br><\/p><\/div><!--\r\na9f73f878f40454a83ceb6bc083488b0 ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871 Jiro>'s\r\n% pick this week is the new\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/heatmap.html |heatmap|>\r\n% function introduced in R2017a.\r\n%\r\n% Last month, a new release of MATLAB (R2017a) came out with many\r\n% <https:\/\/www.mathworks.com\/help\/relnotes\/index.html new features>. Of\r\n% those, I'd like to highlight\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/heatmap.html |heatmap|>, which\r\n% over the years had\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/?term=heatmap&sort=relevancy\r\n% predecessors> created by the user community.\r\n%\r\n% *Meet |heatmap|*\r\n%\r\n% Heatmap charts display data as an image where each element is represented\r\n% by a color. The value of the element is also displayed on the image. As a\r\n% simple example, let's visualize a\r\n% <https:\/\/en.wikipedia.org\/wiki\/Magic_square magic square>.\r\n\r\nm = magic(15);\r\nheatmap(m);\r\n\r\n%%\r\n% You can imagine that if the size of the matrix is large, or if the figure\r\n% is resized to a small window, the image can quickly become crowded with a\r\n% lot of text. No need to worry. The heatmap intelligently turns off the\r\n% text display when it makes sense to do so.\r\n\r\nset(gcf,'Position',[100 100 400 350])\r\n\r\n%%\r\n% |heatmap| returns a\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/heatmapchart-properties.html\r\n% |HeatmapChart|> object, which you can use to customize.\r\n\r\nfigure\r\nhMap = heatmap(m);\r\nhMap.ColorbarVisible = 'off';\r\nhMap.CellLabelColor = 'red';\r\nhMap.GridVisible = 'off';\r\n\r\n%%\r\n% Another feature of |heatmap| that I like is the ability to pass a\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/tables.html table>\r\n% data. For tables, you specify the x and the y variables, and the function\r\n% will calculate and display the aggregated result, such as the count, of\r\n% the various x-y combinations.\r\n%\r\n% For example, if we have a table of data for 100 people with information\r\n% about whether they smoke and their self-assessed health status, we can\r\n% group them into the various categories and visualize as a heatmap.\r\n\r\nload patients\r\ntbl = table(Age,Gender,SelfAssessedHealthStatus,Smoker,Weight,Location);\r\nheatmap(tbl,'Smoker','SelfAssessedHealthStatus');\r\n\r\n%%\r\n% Instead of showing the aggregated count, we can choose to display the\r\n% average age for each category.\r\n\r\nheatmap(tbl,'Smoker','SelfAssessedHealthStatus', ...\r\n    'ColorVariable','Age');\r\n\r\n%%\r\n% Or the median weight.\r\n\r\nheatmap(tbl,'Smoker','SelfAssessedHealthStatus', ...\r\n    'ColorVariable','Weight','ColorMethod','median');\r\n\r\n%%\r\n% *For Previous Releases*\r\n%\r\n% If you haven't upgraded or cannot upgrade to R2017a, you can check out\r\n% some of the\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/?term=heatmap&sort=relevancy\r\n% user-submitted entries>. One of my favorites is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24253-customizable-heat-maps Customizable\r\n% Heat Maps> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/6055857 Ameya>,\r\n% a former collegue of mine.\r\n%\r\n% *Comments*\r\n%\r\n% If you have R2017a, give it a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=8505#respond here>!\r\n\r\n##### SOURCE END ##### a9f73f878f40454a83ceb6bc083488b0\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_r2017a_heatmap\/potw_r2017a_heatmap_01.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\nJiro&#8216;s pick this week is the new heatmap function introduced in R2017a.Last month, a new release of MATLAB (R2017a) came out with many new features. Of those, I&#8217;d like to highlight&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2017\/04\/07\/r2017a-heatmap\/\">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\/8505"}],"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=8505"}],"version-history":[{"count":6,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8505\/revisions"}],"predecessor-version":[{"id":8838,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8505\/revisions\/8838"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=8505"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=8505"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=8505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}