{"id":2568,"date":"2010-06-11T12:55:34","date_gmt":"2010-06-11T12:55:34","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2010\/06\/11\/distribution-plot\/"},"modified":"2017-01-06T21:29:21","modified_gmt":"2017-01-07T02:29:21","slug":"distribution-plot","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2010\/06\/11\/distribution-plot\/","title":{"rendered":"Distribution Plot"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/5021\">Bob<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/23661-violin-plots-for-plotting-multiple-distributions--distributionplot-m-\">Distribution Plot<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/36283\">Jonas<\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <p>The <a href=\"https:\/\/www.mathworks.com\/products\/statistics\/\">Statistics Toolbox<\/a> includes a <tt><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/stats\/boxplot.html\">boxplot<\/a><\/tt> function. The following data shows how the fuel economy of some passenger cars varies with number of cylinders for example.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">load <span style=\"color: #A020F0\">carbig<\/span>\r\nboxplot(MPG,Cylinders)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/pick_distributionplot_01.png\"> <p>The graphical representation of statistical parameters like <tt><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/min.html\">min<\/a><\/tt>, <tt><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/max.html\">max<\/a><\/tt>, <tt><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/mean.html\">mean<\/a><\/tt> and <tt><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/median.html\">median<\/a><\/tt> provide some useful insight.\r\n   <\/p>\r\n   <p>I will reformat the MPG vector into a 2D array for column indexing.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">c = unique(Cylinders);\r\nmpg = nan(numel(MPG),numel(c));\r\n<span style=\"color: #0000FF\">for<\/span> i=1:numel(MPG)\r\n  mpg(i,Cylinders(i)==c) = MPG(i);\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><p>Then I can use <tt>distributionPlot<\/tt> to make a two-dimensional histogram (see also <tt><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/hist.html\">hist<\/a><\/tt>) for comparison. I like heat maps so I'll choose just gray levels.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">distributionPlot(mpg,-1,0,c)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/pick_distributionplot_02.png\"> <p>Notice how I got some other behaviors for free. I intended to use \"hold on\" to overlay both types of plots but Jonas saved\r\n      me a little work. He also chose to draw histogram patches behind my boxplot so I didn't even have to worry about call order.\r\n      I did not expect either, but I like both.\r\n   <\/p>\r\n   <p>Check out his screen shots for more variations such as making the widths of each histogram bin vary with density. Thanks, Jonas!\r\n   <\/p>\r\n   <p><a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2568#respond\">Comments?<\/a><\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_cff271b7c91446bd9cd50c031d501ec5() {\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='cff271b7c91446bd9cd50c031d501ec5 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' cff271b7c91446bd9cd50c031d501ec5';\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 = 'Robert Bemis';\r\n        copyright = 'Copyright 2010 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_cff271b7c91446bd9cd50c031d501ec5()\"><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.10<br><\/p>\r\n<\/div>\r\n<!--\r\ncff271b7c91446bd9cd50c031d501ec5 ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/5021 Bob>'s \r\n% pick this week is \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/23661-violin-plots-for-plotting-multiple-distributions--distributionplot-m- Distribution Plot> \r\n% by \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/36283 Jonas>.\r\n%%\r\n% The \r\n% <https:\/\/www.mathworks.com\/products\/statistics\/ Statistics Toolbox>\r\n% includes a \r\n% |<https:\/\/www.mathworks.com\/help\/releases\/R2010a\/toolbox\/stats\/boxplot.html boxplot>| \r\n% function. The following data shows how the fuel economy of some passenger\r\n% cars varies with number of cylinders for example.\r\nload carbig\r\nboxplot(MPG,Cylinders)\r\n%%\r\n% The graphical representation of statistical parameters like \r\n% |<https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/min.html min>|, \r\n% |<https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/max.html max>|,\r\n% |<https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/mean.html mean>| \r\n% and \r\n% |<https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/median.html median>| \r\n% provide some useful insight. \r\n%%\r\n% I will reformat the MPG vector into a 2D array for column indexing.\r\nc = unique(Cylinders);\r\nmpg = nan(numel(MPG),numel(c));\r\nfor i=1:numel(MPG)\r\n  mpg(i,Cylinders(i)==c) = MPG(i); \r\nend\r\n%%\r\n% Then I can use |distributionPlot| to make a two-dimensional histogram\r\n% (see also \r\n% |<https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/hist.html\r\n% hist>|) for comparison. I like heat maps so I'll choose just gray levels.\r\ndistributionPlot(mpg,-1,0,c)\r\n%%\r\n% Notice how I got some other behaviors for free. I intended to use \"hold\r\n% on\" to overlay both types of plots but Jonas saved me a little work. He\r\n% also chose to draw histogram patches behind my boxplot so I didn't even\r\n% have to worry about call order. I did not expect either, but I like both.\r\n% \r\n%%\r\n% Check out his \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fx_files\/23661\/3\/distributionPlot.png screen shots>\r\n% for more variations such as making the widths of each histogram bin vary\r\n% with density. Thanks, Jonas!\r\n%%\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=2568#respond Comments?>\r\n\r\n##### SOURCE END ##### cff271b7c91446bd9cd50c031d501ec5\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Bob's pick this week is Distribution Plot by Jonas.\r\n      \r\n   \r\n   The Statistics Toolbox includes a boxplot function. The following data shows how the fuel economy of some passenger... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2010\/06\/11\/distribution-plot\/\">read more >><\/a><\/p>","protected":false},"author":46,"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\/2568"}],"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\/46"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=2568"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2568\/revisions"}],"predecessor-version":[{"id":8336,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2568\/revisions\/8336"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}