{"id":6204,"date":"2015-09-26T22:28:50","date_gmt":"2015-09-27T02:28:50","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=6204"},"modified":"2015-09-26T22:37:08","modified_gmt":"2015-09-27T02:37:08","slug":"comparing-multiple-histograms","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2015\/09\/26\/comparing-multiple-histograms\/","title":{"rendered":"Comparing multiple histograms"},"content":{"rendered":"\r\n\r\n<div class=\"content\"><p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27388-plot-and-compare-histograms--pretty-by-default\">\"Comparing Multiple Histograms\"<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/302713\">Jonathan C. Lansey<\/a>.<\/p><p>One of the things you may want to do when analyzing two sets of data is comparing their distributions. There are various ways to do this. One is to fit each data set to a particular distribution using the function <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2015b\/stats\/fitdist.html\"><tt>fistdist<\/tt><\/a> from the <a href=\"https:\/\/www.mathworks.com\/products\/statistics\/\">Statistics and Machine Learning Toolbox<\/a>.<\/p><pre class=\"codeinput\">A.mu_is_Zero = randn(10^5,1);   <span class=\"comment\">% mean of 0<\/span>\r\nA.mu_is_Two = randn(10^5,1)+2;  <span class=\"comment\">% mean of 2<\/span>\r\n\r\n<span class=\"comment\">% This assumes you know the distribution<\/span>\r\ndist1 = fitdist(A.mu_is_Zero,<span class=\"string\">'Normal'<\/span>)  <span class=\"comment\">% fit to a normal distribution<\/span>\r\ndist2 = fitdist(A.mu_is_Two,<span class=\"string\">'Normal'<\/span>)   <span class=\"comment\">% fit to a normal distribution<\/span>\r\n\r\nx = -10:0.1:10;\r\ny1 = pdf(dist1,x);\r\ny2 = pdf(dist2,x);\r\n\r\nplot(x,y1,x,y2)\r\n\r\nlegend(fieldnames(A),<span class=\"string\">'interpreter'<\/span>,<span class=\"string\">'none'<\/span>)\r\n<\/pre><pre class=\"codeoutput\">dist1 = \r\n  NormalDistribution\r\n\r\n  Normal Distribution\r\n       mu = 0.00281815   [-0.00337045, 0.00900676]\r\n    sigma =    0.99848   [0.994123, 1.00288]\r\n\r\ndist2 = \r\n  NormalDistribution\r\n\r\n  Normal Distribution\r\n       mu = 2.00247   [1.99627, 2.00868]\r\n    sigma = 1.00081   [0.996446, 1.00522]\r\n\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_nhist\/potw_nhist_01.png\" alt=\"\"> <p>You can also use <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2015b\/stats\/boxplot.html\"><tt>boxplot<\/tt><\/a>, also from <a href=\"https:\/\/www.mathworks.com\/products\/statistics\/\">Statistics and Machine Learning Toobox<\/a>, to create a box and whisker plot that lets you visualize statistical information.<\/p><pre class=\"codeinput\">clf\r\nboxplot([A.mu_is_Zero, A.mu_is_Two],<span class=\"string\">'labels'<\/span>,fieldnames(A))\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_nhist\/potw_nhist_02.png\" alt=\"\"> <p>Jonathan's <tt>nhist<\/tt> lets you compare the histograms of the data sets easily.<\/p><pre class=\"codeinput\">clf\r\nnhist(A)\r\n<\/pre><pre class=\"codeoutput\">ans = \r\n    mu_is_Zero: 'mu_is_Zero: mean=0.00, std=1.00, 3 points counted in the ...'\r\n     mu_is_Two: 'mu_is_Two: mean=2.00, std=1.00, 1 points counted in the r...'\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_nhist\/potw_nhist_03.png\" alt=\"\"> <p>Note that it automatically uses the field names for the legend.<\/p><p>Of course, this is just the default behavior. This function comes with a wealth of options for controlling everything from line properties and graph orientations to histogram properties and statistics displayed (mode, median, standard error, etc.). For example, to create two separate histograms with a greenish color and same number of bins,<\/p><pre class=\"codeinput\">nhist(A,<span class=\"string\">'color'<\/span>,[.3 .8 .3],<span class=\"string\">'separate'<\/span>,<span class=\"string\">'samebins'<\/span>,<span class=\"string\">'maxbins'<\/span>,50)\r\n<\/pre><pre class=\"codeoutput\">ans = \r\n    mu_is_Zero: 'mu_is_Zero: mean=0.00, std=1.00, 3 points counted in the ...'\r\n     mu_is_Two: 'mu_is_Two: mean=2.00, std=1.00, 1 points counted in the r...'\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_nhist\/potw_nhist_04.png\" alt=\"\"> <p><b>Comments<\/b><\/p><p>There are many other options and the function comes with a very detailed help. Give it a try, and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=6204#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27388-plot-and-compare-histograms--pretty-by-default#comments\">comment<\/a> for Jonathan.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_d64eb7f6b16e451eb3232bc1ec8c12ff() {\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='d64eb7f6b16e451eb3232bc1ec8c12ff ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' d64eb7f6b16e451eb3232bc1ec8c12ff';\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 2015 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_d64eb7f6b16e451eb3232bc1ec8c12ff()\"><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; R2015b<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2015b<br><\/p><\/div><!--\r\nd64eb7f6b16e451eb3232bc1ec8c12ff ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27388-plot-and-compare-histograms--pretty-by-default \"Comparing\r\n% Multiple Histograms\"> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/302713 Jonathan\r\n% C. Lansey>.\r\n%\r\n% One of the things you may want to do when analyzing two sets of data is\r\n% comparing their distributions. There are various ways to do this. One is\r\n% to fit each data set to a particular distribution using the function\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2015b\/stats\/fitdist.html\r\n% |fistdist|> from the <https:\/\/www.mathworks.com\/products\/statistics\/\r\n% Statistics and Machine Learning Toolbox>.\r\n\r\nA.mu_is_Zero = randn(10^5,1);   % mean of 0\r\nA.mu_is_Two = randn(10^5,1)+2;  % mean of 2\r\n\r\n% This assumes you know the distribution\r\ndist1 = fitdist(A.mu_is_Zero,'Normal')  % fit to a normal distribution\r\ndist2 = fitdist(A.mu_is_Two,'Normal')   % fit to a normal distribution\r\n\r\nx = -10:0.1:10;\r\ny1 = pdf(dist1,x);\r\ny2 = pdf(dist2,x);\r\n\r\nplot(x,y1,x,y2)\r\n\r\nlegend(fieldnames(A),'interpreter','none')\r\n\r\n%%\r\n% You can also use\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2015b\/stats\/boxplot.html\r\n% |boxplot|>, also from <https:\/\/www.mathworks.com\/products\/statistics\/\r\n% Statistics and Machine Learning Toobox>, to create a box and whisker plot\r\n% that lets you visualize statistical information.\r\n\r\nclf\r\nboxplot([A.mu_is_Zero, A.mu_is_Two],'labels',fieldnames(A))\r\n\r\n%%\r\n% Jonathan's |nhist| lets you compare the histograms of the data sets\r\n% easily.\r\n\r\nclf\r\nnhist(A)\r\n\r\n%%\r\n% Note that it automatically uses the field names for the legend.\r\n%\r\n% Of course, this is just the default behavior. This function comes with a\r\n% wealth of options for controlling everything from line properties and\r\n% graph orientations to histogram properties and statistics displayed\r\n% (mode, median, standard error, etc.). For example, to create two separate\r\n% histograms with a greenish color and same number of bins,\r\n\r\nnhist(A,'color',[.3 .8 .3],'separate','samebins','maxbins',50)\r\n\r\n%%\r\n% *Comments*\r\n%\r\n% There are many other options and the function comes with a very detailed\r\n% help. Give it a try, and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=6204#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27388-plot-and-compare-histograms--pretty-by-default#comments\r\n% comment> for Jonathan.\r\n\r\n##### SOURCE END ##### d64eb7f6b16e451eb3232bc1ec8c12ff\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_nhist\/potw_nhist_01.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n\r\nJiro's pick this week is \"Comparing Multiple Histograms\" by Jonathan C. Lansey.One of the things you may want to do when analyzing two sets of data is comparing their distributions. There are... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2015\/09\/26\/comparing-multiple-histograms\/\">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\/6204"}],"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=6204"}],"version-history":[{"count":6,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6204\/revisions"}],"predecessor-version":[{"id":6210,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6204\/revisions\/6210"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=6204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=6204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=6204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}