{"id":6044,"date":"2015-06-05T09:00:24","date_gmt":"2015-06-05T13:00:24","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=6044"},"modified":"2015-06-04T23:24:59","modified_gmt":"2015-06-05T03:24:59","slug":"xkcdify","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2015\/06\/05\/xkcdify\/","title":{"rendered":"XKCDIFY"},"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\/38499-xkcdify\"><tt>xkcdify<\/tt><\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1526835\">Stuart Layton<\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <p>MATLAB has lots of stock plotting routines and many more that come with the various toolboxes.  You can see the ones available\r\n      to you on the plots tab:\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainxkcdify\/plottab.png\"> <\/p>\r\n   <p>However, all of these plots are very formal.  MATLAB doesn't have any stock options for informal plotting.  The comic strip\r\n      <a href=\"http:\/\/www.xkcd.com\">xkcd<\/a> does a very good job communicating data in an informal way and Stuart has given us <tt>xkcdify<\/tt> to take our existing MATLAB plots and xkcdify or informalize them.\r\n   <\/p>\r\n   <p>We've had some unseasonably cold weather in Massachusetts over the last few days (surprise I know!).  Let's take a look at\r\n      it with a control chart before and after xkcdification.  I'll use the temperatures that I'm comfortable in a t-shirt and shorts\r\n      for as the limits.\r\n   <\/p>\r\n   <p>Before<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">controlchart(Temperature,<span style=\"color: #A020F0\">'limit'<\/span>,[50 68 85])\r\nylabel(<span style=\"color: #A020F0\">'Temperature ^\\circF'<\/span>)\r\nylim([45 90])\r\ntext(1,47,<span style=\"color: #A020F0\">'Too Cold'<\/span>)\r\ntext(1,87,<span style=\"color: #A020F0\">'Too Hot'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainxkcdify\/mainxkcdify_01.png\"> <p>After<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">xkcdify(gca)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainxkcdify\/mainxkcdify_02.png\"> <p>If this isn't informal enough, we can re-xkcdify too!<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">xkcdify(gca)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainxkcdify\/mainxkcdify_03.png\"> <p>There are a couple changes I made to <tt>xkcdify<\/tt> to make it work on more plot types.\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li>To separate a line into multiple pieces, you can use NaN.  For the length of line calculation, I removed these NaNs because\r\n            otherwise the line length was always NaN.\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Line 209:210<\/span>\r\ngoodIdx = ~isnan(x) &amp; ~isnan(y) &amp; isfinite(x) &amp; isfinite(y);\r\nlen = sum(hypot(diff(x(goodIdx)),diff(y(goodIdx))));<\/pre><div>\r\n      <ul>\r\n         <li>There are cases where a line can be empty so I put an <tt>isempty()<\/tt> check around this.\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Line 129:132<\/span>\r\n<span style=\"color: #0000FF\">if<\/span> ~isempty(x)\r\n    x = x + smooth( generateNoise(n) .* rand(n,1) .* jx )';\r\n    y = y + smooth( generateNoise(n) .* rand(n,1) .* jy )';\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><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=6044#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/38499-xkcdify#comments\">comment<\/a> for Stuart.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_a0994901ea7a4578835a9275a0cc4347() {\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='a0994901ea7a4578835a9275a0cc4347 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' a0994901ea7a4578835a9275a0cc4347';\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 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 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_a0994901ea7a4578835a9275a0cc4347()\"><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; R2015a<br><\/p>\r\n<\/div>\r\n<!--\r\na0994901ea7a4578835a9275a0cc4347 ##### SOURCE BEGIN #####\r\n%% XKCDIFY\r\n%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495 Sean>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/38499-xkcdify |xkcdify|> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1526835 Stuart Layton>.\r\n% \r\n\r\n%% \r\n% MATLAB has lots of stock plotting routines and many more that come with\r\n% the various toolboxes.  You can see the ones available to you on the\r\n% plots tab:\r\n%\r\n% <<plottab.png>>\r\n%\r\n% However, all of these plots are very formal.  MATLAB doesn't have any\r\n% stock options for informal plotting.  The comic strip\r\n% <http:\/\/www.xkcd.com xkcd> does a very good job communicating data in an\r\n% informal way and Stuart has given us |xkcdify| to take our existing\r\n% MATLAB plots and xkcdify or informalize them.\r\n% \r\n% We've had some unseasonably cold weather in Massachusetts over the last\r\n% few days (surprise I know!).  Let's take a look at it with a control\r\n% chart before and after xkcdification.  I'll use the temperatures that I'm\r\n% comfortable in a t-shirt and shorts for as the limits.\r\n%\r\n\r\n%%\r\n% Before\r\n\r\ncontrolchart(Temperature,'limit',[50 68 85])\r\nylabel('Temperature ^\\circF')\r\nylim([45 90])\r\ntext(1,47,'Too Cold')\r\ntext(1,87,'Too Hot')\r\n\r\n%%\r\n% After\r\n\r\nxkcdify(gca)\r\n\r\n%%\r\n% If this isn't informal enough, we can re-xkcdify too!\r\n\r\nxkcdify(gca)\r\n\r\n%%\r\n% There are a couple changes I made to |xkcdify| to make it work on more plot\r\n% types.\r\n%\r\n% * To separate a line into multiple pieces, you can use NaN.  For the length of\r\n% line calculation, I removed these NaNs because otherwise the line length\r\n% was always NaN.\r\n%\r\n%   % Line 209:210\r\n%   goodIdx = ~isnan(x) & ~isnan(y) & isfinite(x) & isfinite(y);  \r\n%   len = sum(hypot(diff(x(goodIdx)),diff(y(goodIdx))));\r\n%\r\n% * There are cases where a line can be empty so I put an |isempty()| check\r\n% around this.\r\n%\r\n%   % Line 129:132\r\n%   if ~isempty(x)\r\n%       x = x + smooth( generateNoise(n) .* rand(n,1) .* jx )';\r\n%       y = y + smooth( generateNoise(n) .* rand(n,1) .* jy )';\r\n%   end\r\n%\r\n%% Comments\r\n% \r\n% Give it a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=6044#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/38499-xkcdify#comments\r\n% comment> for Stuart.\r\n \r\n\r\n##### SOURCE END ##### a0994901ea7a4578835a9275a0cc4347\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/Sean\/mainxkcdify\/plottab.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n   \r\n      Sean's pick this week is xkcdify by Stuart Layton.\r\n      \r\n   \r\n   MATLAB has lots of stock plotting routines and many more that come with the various toolboxes.  You can see... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2015\/06\/05\/xkcdify\/\">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\/6044"}],"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=6044"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6044\/revisions"}],"predecessor-version":[{"id":6045,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/6044\/revisions\/6045"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=6044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=6044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=6044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}