{"id":7680,"date":"2016-07-08T09:00:43","date_gmt":"2016-07-08T13:00:43","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=7680"},"modified":"2017-08-11T13:25:30","modified_gmt":"2017-08-11T17:25:30","slug":"alternative-box-plot","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2016\/07\/08\/alternative-box-plot\/","title":{"rendered":"Alternative Box 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\/profile\/authors\/3208495\">Sean<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/46545-alternative-box-plot\">Alternative Box Plot<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2544496\">Christopher Hummersone<\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n\r\n   <h3>Background<a name=\"1\"><\/a><\/h3>\r\n   <p>I was recently doing a seminar for some MATLAB users where one of the plots I showed was a <tt><a href=\"https:\/\/www.mathworks.com\/help\/stats\/boxplot.html\">boxplot<\/a><\/tt>. The box plot was used to quickly get a feel for the distributions of some energy data based on time of day.  Here it is:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">figure\r\nboxplot(Power, Hour, <span style=\"color: #A020F0\">'Notch'<\/span>, <span style=\"color: #A020F0\">'on'<\/span>)\r\nxlabel(<span style=\"color: #A020F0\">'Hour of Day'<\/span>)\r\nylabel(<span style=\"color: #A020F0\">'Power (MW)'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainAltBoxPlot\/mainAltBoxPlot_01.png\"> <p>There is a lot of information in this plot.  The whiskers are the limits excluding outliers which are the red +s.  The boxes\r\n      represent the 25th and 75th percentiles, the red line the median, and the notches tell you if two medians are significantly\r\n      different if they don't overlap.\r\n   <\/p>\r\n   <p>An audience member then asked me if there was a way to control the box limits to be an arbitrary percentile, they wanted 10%\r\n      and 90%.  I searched the parameter-value pairs but did not find anything.\r\n   <\/p>\r\n   <h3>Enter Alternative Box Plot!<a name=\"3\"><\/a><\/h3>\r\n   <p>Alternative box plot provides many of the same options as MATLAB's boxplot but also a few more and the percentile is one of\r\n      them.\r\n   <\/p>\r\n   <p>Christopher has his tools in a package, which is nice to avoid naming conflicts with other functions.  I'll <tt><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/import.html\">import<\/a><\/tt> the package to use the short name:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">import <span style=\"color: #A020F0\">iosr.statistics.*<\/span>\r\ny = tab2box(Hour,Power); <span style=\"color: #228B22\">% reshapes to be boxPlot compliant<\/span>\r\nbp = boxPlot(0:23,y,<span style=\"color: #A020F0\">'Notch'<\/span>,true,<span style=\"color: #A020F0\">'Percentile'<\/span>,[10 90]);<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainAltBoxPlot\/mainAltBoxPlot_02.png\"> <p>Very nice!<\/p>\r\n   <p>You may have also noticed that I grabbed an output argument.  This allows me to customize the box plot with easy tab completion\r\n      after it's already made.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bp.lineColor = <span style=\"color: #A020F0\">'b'<\/span>;\r\nbp.medianColor = <span style=\"color: #A020F0\">'r'<\/span>;<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainAltBoxPlot\/mainAltBoxPlot_03.png\"> <p>In fact, I could even have all four percentiles by adding additional ones.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bp = boxPlot(0:23,y,<span style=\"color: #A020F0\">'Notch'<\/span>,true);\r\nbp.addPrctiles = [10 90];\r\nbp.addPrctilesColors = {<span style=\"color: #A020F0\">'r'<\/span>;<span style=\"color: #A020F0\">'b'<\/span>}<\/pre><pre style=\"font-style:oblique\">bp = \r\n  boxPlot with properties:\r\n\r\n           addPrctiles: [10 90]\r\n     addPrctilesColors: {2&times;1 cell}\r\n     addPrctilesLabels: {2&times;1 cell}\r\n    addPrctilesMarkers: {2&times;1 cell}\r\n    addPrctilesTxtSize: []\r\n              boxAlpha: 1\r\n              boxColor: {'none'}\r\n              boxWidth: 'auto'\r\n    groupLabelFontSize: 9\r\n      groupLabelHeight: 'auto'\r\n           groupLabels: {1&times;0 cell}\r\n            groupWidth: 0.7500\r\n                 limit: '1.5IQR'\r\n             lineColor: {'k'}\r\n             lineStyle: {'-'}\r\n             lineWidth: 1\r\n             meanColor: {[0 0.4470 0.7410]}\r\n            meanMarker: {'+'}\r\n              meanSize: 6\r\n           medianColor: {[0 0.4470 0.7410]}\r\n                method: 'R-5'\r\n                 notch: 1\r\n            notchDepth: 0.4000\r\n        notchLineColor: {'k'}\r\n        notchLineStyle: {':'}\r\n             notchLine: 0\r\n           outlierSize: 36\r\n            percentile: [25 75]\r\n        sampleFontSize: 9\r\n            sampleSize: 0\r\n            scaleWidth: 0\r\n          scatterAlpha: 1\r\n          scatterColor: {[0.5000 0.5000 0.5000]}\r\n          scatterLayer: 'top'\r\n         scatterMarker: {'x'}\r\n           scatterSize: 36\r\n            showLegend: 0\r\n              showMean: 0\r\n          showOutliers: 1\r\n           showScatter: 0\r\n                 style: 'normal'\r\n           symbolColor: {[0 0.4470 0.7410]}\r\n          symbolMarker: {'o'}\r\n                 theme: 'default'\r\n            xSeparator: 0\r\n              xSpacing: 'x'\r\n               handles: [1&times;1 struct]\r\n                     x: [1&times;24 double]\r\n                     y: [334&times;24 double]\r\n               weights: [334&times;24 double]\r\n            statistics: [1&times;1 struct]\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainAltBoxPlot\/mainAltBoxPlot_04.png\"> <h3>Comments<a name=\"6\"><\/a><\/h3>\r\n   <p>Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=7680#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/46545-alternative-box-plot#comments\">comment<\/a> for Christopher.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_e21d0bc79abb437eba2b316757e7b608() {\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='e21d0bc79abb437eba2b316757e7b608 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' e21d0bc79abb437eba2b316757e7b608';\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 = 'Sean de Wolski';\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 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_e21d0bc79abb437eba2b316757e7b608()\"><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; R2016b<br><\/p>\r\n<\/div>\r\n<!--\r\ne21d0bc79abb437eba2b316757e7b608 ##### SOURCE BEGIN #####\r\n%% Alternative Box Plot\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495 Sean>'s pick \r\n% this week is <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/46545-alternative-box-plot Alternative \r\n% Box Plot> by <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2544496 \r\n% Christopher Hummersone>.\r\n\r\n%% Background\r\n% I was recently doing a seminar for some MATLAB users where one of the plots \r\n% I showed was a |<https:\/\/www.mathworks.com\/help\/stats\/boxplot.html boxplot>|.  \r\n% The box plot was used quickly get a feel for the distributions of some energy \r\n% data based on time of day.  Here it is:\r\n\r\nfigure\r\nboxplot(Power, Hour, 'Notch', 'on')\r\nxlabel('Hour of Day')\r\nylabel('Power (MW)')\r\n%% \r\n% There is a lot of information in this plot.  The whiskers are the limits \r\n% excluding outliers which are the red +s.  The boxes represent the 25th and 75th \r\n% percentiles, the red line the mean, and the notches tell you if two medians \r\n% are significantly different if they don't overlap.  \r\n% \r\n% An audience member then asked me if there was a way to control the box \r\n% limits to be an arbitrary percentile, they wanted 10% and 90%.  I searched the \r\n% parameter-value pairs but did not find anything.  \r\n%% *Enter Alternative Box Plot!*\r\n% Alternative box plot provides many of the same options as MATLAB's boxplot \r\n% but also a few more and the percentile is one of them.\r\n% \r\n% Christopher has his tools in a package, which is nice to avoid naming conflicts \r\n% with other functions.  I'll |<https:\/\/www.mathworks.com\/help\/matlab\/ref\/import.html \r\n% import>| the package to use the short name:\r\n\r\nimport iosr.statistics.*\r\ny = tab2box(Hour,Power); % reshapes to be boxPlot compliant\r\nbp = boxPlot(0:23,y,'Notch',true,'Percentile',[10 90]);\r\n%% \r\n% Very nice!\r\n% \r\n% You may have also noticed that I grabbed an output argument.  This allows \r\n% me to customize the box plot with easy tab completion after it's already made.\r\n\r\nbp.lineColor = 'b';\r\nbp.medianColor = 'r';\r\n%% \r\n% In fact, I could even have all four percentiles by adding additional ones.\r\n\r\nbp = boxPlot(0:23,y,'Notch',true);\r\nbp.addPrctiles = [10 90];\r\nbp.addPrctilesColors = {'r';'b'}\r\n%% Comments\r\n% Give it a try and let us know what you think <https:\/\/blogs.mathworks.com\/pick\/ \r\n% here> or leave a <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/46545-alternative-box-plot#comments \r\n% comment> for Christopher.\r\n##### SOURCE END ##### e21d0bc79abb437eba2b316757e7b608\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainAltBoxPlot\/mainAltBoxPlot_01.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n   \r\n      Sean's pick this week is Alternative Box Plot by Christopher Hummersone.\r\n      \r\n   \r\n\r\n   Background\r\n   I was recently doing a seminar for some MATLAB users where one of the plots... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2016\/07\/08\/alternative-box-plot\/\">read more >><\/a><\/p>","protected":false},"author":87,"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\/7680"}],"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\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=7680"}],"version-history":[{"count":10,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/7680\/revisions"}],"predecessor-version":[{"id":8813,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/7680\/revisions\/8813"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=7680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=7680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=7680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}