{"id":3798,"date":"2012-08-17T06:30:40","date_gmt":"2012-08-17T11:30:40","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=3798"},"modified":"2012-08-17T06:30:40","modified_gmt":"2012-08-17T11:30:40","slug":"shaded-error-bars","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2012\/08\/17\/shaded-error-bars\/","title":{"rendered":"Shaded Error Bars"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <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\/26311-raacampbell-shadederrorbar\"><tt>shadedErrorBar<\/tt><\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/49773\">Rob Campbell<\/a>.\r\n   <\/p>\r\n   <p>This one was <a href=\"https:\/\/blogs.mathworks.com\/pick\/2012\/04\/13\/what-is-your-favorite-unrecognized-file-exchange-submission\/#comment-14622\">suggested<\/a> to us by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/29192\">Oliver<\/a>, but I'm pretty certain I would have picked it if I had come across this entry.\r\n   <\/p>\r\n   <p>In fact, we used to (try to) make similar plots back in my graduate school days. We had some time series data from multiple\r\n      subjects, and we wanted a way to see trends and variability. It took a little while for me to arrive at this way of visualizing\r\n      variability. Initially, I would simply plot the line series and visually inspect how close or separated the lines were. We\r\n      already had quantitative measures, but I also wanted the visual representation.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% From one of Rob's examples<\/span>\r\nx=(1:80)-40;\r\ny=ones(15,1)*x; y=y+0.06*y.^2+randn(size(y))*10;\r\n\r\nplot(x, y);<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_shadederrorbar\/potw_shadederrorbar_01.png\"> <p>Next, I simply created lines to represent the mean and multiple standard deviations. This was better, but it was still a bit\r\n      crowded visually.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">mean_y = mean(y);\r\nstd_y = std(y);\r\n\r\nfigure;hold <span style=\"color: #A020F0\">on<\/span>;\r\n\r\nH1 = plot(x, mean_y, <span style=\"color: #A020F0\">'Color'<\/span>, <span style=\"color: #A020F0\">'k'<\/span>, <span style=\"color: #A020F0\">'LineWidth'<\/span>, 2);\r\nH2 = plot(x, [mean_y - 2*std_y; mean_y + 2*std_y], <span style=\"color: #A020F0\">'Color'<\/span>, <span style=\"color: #A020F0\">'r'<\/span>);\r\nH3 = plot(x, [mean_y - std_y; mean_y + std_y], <span style=\"color: #A020F0\">'Color'<\/span>, <span style=\"color: #A020F0\">'m'<\/span>);\r\nH4 = plot(x, [mean_y - 0.5*std_y  ; mean_y +   0.5*std_y], <span style=\"color: #A020F0\">'Color'<\/span>, <span style=\"color: #A020F0\">'b'<\/span>);\r\n\r\nlegend([H1, H2(1), H3(1), H4(1)], <span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'\\mu'<\/span>, <span style=\"color: #A020F0\">'2\\sigma'<\/span>, <span style=\"color: #A020F0\">'\\sigma'<\/span>, <span style=\"color: #A020F0\">'0.5\\sigma'<\/span>, <span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'Location'<\/span>, <span style=\"color: #A020F0\">'Northwest'<\/span>);<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_shadederrorbar\/potw_shadederrorbar_02.png\"> <p>Finally, we decided on a shaded representation of bounds. I don't remember how complicated the code was, but with Rob's <tt>shadedErrorBar<\/tt>, I can do this very easily!\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">figure;hold <span style=\"color: #A020F0\">on<\/span>;\r\n\r\nH(1) = shadedErrorBar(x, y, {@mean, @(x) 2*std(x)  }, <span style=\"color: #A020F0\">'-r'<\/span>, 0);\r\nH(2) = shadedErrorBar(x, y, {@mean, @(x) 1*std(x)  }, <span style=\"color: #A020F0\">'-m'<\/span>, 0);\r\nH(3) = shadedErrorBar(x, y, {@mean, @(x) 0.5*std(x)}, {<span style=\"color: #A020F0\">'-b'<\/span>, <span style=\"color: #A020F0\">'LineWidth'<\/span>, 2}, 0);\r\n\r\nlegend([H(3).mainLine, H.patch], <span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'\\mu'<\/span>, <span style=\"color: #A020F0\">'2\\sigma'<\/span>, <span style=\"color: #A020F0\">'\\sigma'<\/span>, <span style=\"color: #A020F0\">'0.5\\sigma'<\/span>, <span style=\"color: #0000FF\">...<\/span>\r\n    <span style=\"color: #A020F0\">'Location'<\/span>, <span style=\"color: #A020F0\">'Northwest'<\/span>);<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_shadederrorbar\/potw_shadederrorbar_03.png\"> <p>The code is well-written, with help text, examples, error-checking, and lots of comments &#8211; everything that I look for in a\r\n      good MATLAB code! Thanks for your entry, Rob, and thanks Oliver for the suggestion!\r\n   <\/p>\r\n   <p><b>Comments<\/b><\/p>\r\n   <p>Let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=3798#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/26311-raacampbell-shadederrorbar#comments\">comment<\/a> for Rob.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_3e99b587a6a44e7ea902f160a3586771() {\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='3e99b587a6a44e7ea902f160a3586771 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 3e99b587a6a44e7ea902f160a3586771';\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 = 'Jiro Doke';\r\n        copyright = 'Copyright 2012 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_3e99b587a6a44e7ea902f160a3586771()\"><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.14<br><\/p>\r\n<\/div>\r\n<!--\r\n3e99b587a6a44e7ea902f160a3586771 ##### 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\/26311-raacampbell-shadederrorbar\r\n% |shadedErrorBar|> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/49773 Rob\r\n% Campbell>.\r\n%\r\n% This one was\r\n% <https:\/\/blogs.mathworks.com\/pick\/2012\/04\/13\/what-is-your-favorite-unrecognized-file-exchange-submission\/#comment-14622\r\n% suggested> to us by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/29192\r\n% Oliver>, but I'm pretty certain I would have picked it if I had come\r\n% across this entry.\r\n%\r\n% In fact, we used to (try to) make similar plots back in my graduate\r\n% school days. We had some time series data from multiple subjects, and we\r\n% wanted a way to see trends and variability. It took a little while for me\r\n% to arrive at this way of visualizing variability. Initially, I would\r\n% simply plot the line series and visually inspect how close or separated\r\n% the lines were. We already had quantitative measures, but I also wanted\r\n% the visual representation.\r\n\r\n% From one of Rob's examples\r\nx=(1:80)-40;\r\ny=ones(15,1)*x; y=y+0.06*y.^2+randn(size(y))*10;\r\n\r\nplot(x, y);\r\n\r\n%%\r\n% Next, I simply created lines to represent the mean and multiple standard\r\n% deviations. This was better, but it was still a bit crowded visually.\r\n\r\nmean_y = mean(y);\r\nstd_y = std(y);\r\n\r\nfigure;hold on;\r\n\r\nH1 = plot(x, mean_y, 'Color', 'k', 'LineWidth', 2);\r\nH2 = plot(x, [mean_y - 2*std_y; mean_y + 2*std_y], 'Color', 'r');\r\nH3 = plot(x, [mean_y - std_y; mean_y + std_y], 'Color', 'm');\r\nH4 = plot(x, [mean_y - 0.5*std_y  ; mean_y +   0.5*std_y], 'Color', 'b');\r\n\r\nlegend([H1, H2(1), H3(1), H4(1)], ...\r\n    '\\mu', '2\\sigma', '\\sigma', '0.5\\sigma', ...\r\n    'Location', 'Northwest');\r\n\r\n%%\r\n% Finally, we decided on a shaded representation of bounds. I don't\r\n% remember how complicated the code was, but with Rob's |shadedErrorBar|,\r\n% I can do this very easily!\r\n\r\nfigure;hold on;\r\n\r\nH(1) = shadedErrorBar(x, y, {@mean, @(x) 2*std(x)  }, '-r', 0);\r\nH(2) = shadedErrorBar(x, y, {@mean, @(x) 1*std(x)  }, '-m', 0);\r\nH(3) = shadedErrorBar(x, y, {@mean, @(x) 0.5*std(x)}, {'-b', 'LineWidth', 2}, 0);\r\n\r\nlegend([H(3).mainLine, H.patch], ...\r\n    '\\mu', '2\\sigma', '\\sigma', '0.5\\sigma', ...\r\n    'Location', 'Northwest');\r\n\r\n%%\r\n% The code is well-written, with help text, examples, error-checking, and\r\n% lots of comments \u00e2\u20ac\u201c everything that I look for in a good MATLAB code!\r\n% Thanks for your entry, Rob, and thanks Oliver for the suggestion!\r\n%\r\n% *Comments*\r\n%\r\n% Let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=3798#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/26311-raacampbell-shadederrorbar#comments\r\n% comment> for Rob.\r\n\r\n##### SOURCE END ##### 3e99b587a6a44e7ea902f160a3586771\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Jiro's pick this week is shadedErrorBar by Rob Campbell.\r\n   \r\n   This one was suggested to us by Oliver, but I'm pretty certain I would have picked it if I had come across this entry.\r\n   \r\n  ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2012\/08\/17\/shaded-error-bars\/\">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\/3798"}],"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=3798"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/3798\/revisions"}],"predecessor-version":[{"id":3802,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/3798\/revisions\/3802"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=3798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=3798"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=3798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}