{"id":2916,"date":"2011-07-15T12:45:48","date_gmt":"2011-07-15T12:45:48","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2011\/07\/15\/creating-hatched-patches\/"},"modified":"2011-07-15T12:45:48","modified_gmt":"2011-07-15T12:45:48","slug":"creating-hatched-patches","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2011\/07\/15\/creating-hatched-patches\/","title":{"rendered":"Creating Hatched Patches"},"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\/15007\">Jiro<\/a>'s picks this week are <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/30733-hatchfill\"><tt>hatchfill<\/tt><\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/129234\">Neil Tandon<\/a>, <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24021-hatch-fill-patterns-plus-color---invert\"><tt>applyhatch_pluscolor<\/tt><\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/56270\">Brandon Levey<\/a>, and <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/26797-applyhatch-plusc\"><tt>applyhatch_plusC<\/tt><\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/8955\">Brian Katz<\/a>.\r\n      <\/p>\r\n      <p>In graduate school, I used to spend some time preparing my plots for publication. One of the requirements was that the plots\r\n         needed to be legible and comprehensible in black and white. For line plots, I made use of various line <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/line_props.html#LineStyle\">styles<\/a>, <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/line_props.html#LineWidth\">thicknesses<\/a>, and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/line_props.html#Marker\">markers<\/a>. For patches, I would use gray scale to represent the different colors. In the days of monochrome screens and low resolution\r\n         dot-matrix printers, hatches used to be the way to represent various patch regions. With functions like the ones by Neil,\r\n         Brandon, and Brian, you too can create hatches easily in MATLAB.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">hatchfill<\/a><\/li>\r\n         <li><a href=\"#3\">applyhatch_pluscolor and applyhatch_plusC<\/a><\/li>\r\n         <li><a href=\"#5\">Comments<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>hatchfill<a name=\"1\"><\/a><\/h3>\r\n   <p>Neil's function allows you to pass in patch handles and convert them into hatches that are customizable. Let's look at this\r\n      contour plot which contains several sections that I want to replace with hatches.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Create two data sets<\/span>\r\nmData = membrane(1, 50);\r\npData = peaks(101)-10;\r\n\r\n<span style=\"color: #228B22\">% Set colormap<\/span>\r\ncolormap([0 0 0; summer; 1 1 1])\r\ncontourf(mData);\r\ncaxis([min(mData(:)), max(mData(:))]);\r\n\r\nhold <span style=\"color: #A020F0\">on<\/span>\r\n[c1, h1] = contourf(pData, [2.5 2.5]-10);\r\n[c2, h2] = contourf(-pData, [2.5 2.5]+10);\r\nhold <span style=\"color: #A020F0\">off<\/span>;<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_hatchfill\/potw_hatchfill_01.png\"> <p>I can make one of them a diagonal hatch and the other one a cross hatch.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Get patch objects from CONTOURGROUP<\/span>\r\nhPatch1 = findobj(h1, <span style=\"color: #A020F0\">'Type'<\/span>, <span style=\"color: #A020F0\">'patch'<\/span>);\r\nhPatch2 = findobj(h2, <span style=\"color: #A020F0\">'Type'<\/span>, <span style=\"color: #A020F0\">'patch'<\/span>);\r\n\r\n<span style=\"color: #228B22\">% Apply Hatch Fill<\/span>\r\nhh1 = hatchfill(hPatch1, <span style=\"color: #A020F0\">'single'<\/span>, -45, 3);\r\nhh2 = hatchfill(hPatch2, <span style=\"color: #A020F0\">'cross'<\/span>, 45, 3);\r\n\r\n<span style=\"color: #228B22\">% Remove outline<\/span>\r\nset([h1, h2], <span style=\"color: #A020F0\">'LineStyle'<\/span>, <span style=\"color: #A020F0\">'none'<\/span>)\r\n\r\n<span style=\"color: #228B22\">% Change the cross hatch to white<\/span>\r\nset(hh2, <span style=\"color: #A020F0\">'Color'<\/span>, <span style=\"color: #A020F0\">'white'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_hatchfill\/potw_hatchfill_02.png\"> <h3>applyhatch_pluscolor and applyhatch_plusC<a name=\"3\"><\/a><\/h3>\r\n   <p>Both Brandon's and Brian's entries convert the distinct colors in the whole figure into hatch patterns. The nice thing about\r\n      this is that they will also take care of legends.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Create original plot<\/span>\r\nfH = gcf; colormap(jet(4));\r\nh = bar(rand(3, 4));\r\nlegend(<span style=\"color: #A020F0\">'Apple'<\/span>, <span style=\"color: #A020F0\">'Orange'<\/span>, <span style=\"color: #A020F0\">'Banana'<\/span>, <span style=\"color: #A020F0\">'Melon'<\/span>, <span style=\"color: #A020F0\">'Location'<\/span>, <span style=\"color: #A020F0\">'EastOutside'<\/span>);\r\n\r\n<span style=\"color: #228B22\">% Apply Brandon's function<\/span>\r\ntH = title(<span style=\"color: #A020F0\">'Brandon''s applyhatch'<\/span>);\r\napplyhatch_pluscolor(fH, <span style=\"color: #A020F0\">'\\-x.'<\/span>, 0, [1 0 1 0], jet(4));\r\n\r\n<span style=\"color: #228B22\">% Apply Brian's function<\/span>\r\nset(tH, <span style=\"color: #A020F0\">'String'<\/span>, <span style=\"color: #A020F0\">'Brian''s applyhatch'<\/span>);\r\napplyhatch_plusC(fH, <span style=\"color: #A020F0\">'\\-x.'<\/span>, <span style=\"color: #A020F0\">'rkbk'<\/span>);\r\n\r\nset(tH, <span style=\"color: #A020F0\">'String'<\/span>, <span style=\"color: #A020F0\">'Original'<\/span>);<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_hatchfill\/potw_hatchfill_03.png\"> <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_hatchfill\/potw_hatchfill_04.png\"> <img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_hatchfill\/potw_hatchfill_05.png\"> <p>Both of these functions were derived and inspired by some of the same existing entries, so the functionalities are quite similar,\r\n      with some differences in syntax. For example, Brandon's function has a couple of convenience syntaxes for inverting foreground\/background\r\n      colors and specifying different hatch line width. Brian's function can assign arbitrary mix of colors to the hatches regardless\r\n      of the original colors used.\r\n   <\/p>\r\n   <p>I just highlighted three entries from File Exchange for creating hatches, but there are a few other <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/?term=hatching\">entries<\/a> that you may want to check out.\r\n   <\/p>\r\n   <h3>Comments<a name=\"5\"><\/a><\/h3>\r\n   <p>Let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2916#respond\">here<\/a> or leave a comment for <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/30733-hatchfill#comments\">Neil<\/a>, <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24021-hatch-fill-patterns-plus-color---invert#comments\">Brandon<\/a>, and <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/26797-applyhatch-plusc#comments\">Brian<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_bd0ef3038e0d425e9eb45bc17896df4d() {\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='bd0ef3038e0d425e9eb45bc17896df4d ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' bd0ef3038e0d425e9eb45bc17896df4d';\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 2011 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_bd0ef3038e0d425e9eb45bc17896df4d()\"><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.12<br><\/p>\r\n<\/div>\r\n<!--\r\nbd0ef3038e0d425e9eb45bc17896df4d ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s picks this week are\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/30733-hatchfill |hatchfill|>\r\n% by <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/129234\r\n% Neil Tandon>, <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24021-hatch-fill-patterns-plus-color---invert\r\n% |applyhatch_pluscolor|> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/56270\r\n% Brandon Levey>, and\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/26797-applyhatch-plusc\r\n% |applyhatch_plusC|> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/8955 Brian\r\n% Katz>.\r\n%\r\n% In graduate school, I used to spend some time preparing my plots for\r\n% publication. One of the requirements was that the plots needed to be\r\n% legible and comprehensible in black and white. For line plots, I made use\r\n% of various line\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/line_props.html#LineStyle\r\n% styles>,\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/line_props.html#LineWidth\r\n% thicknesses>, and\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011a\/techdoc\/\/ref\/line_props.html#Marker\r\n% markers>. For patches, I would use gray scale to represent the different\r\n% colors. In the days of monochrome screens and low resolution dot-matrix\r\n% printers, hatches used to be the way to represent various patch regions.\r\n% With functions like the ones by Neil, Brandon, and Brian, you too can\r\n% create hatches easily in MATLAB.\r\n\r\n%% |hatchfill|\r\n%\r\n% Neil's function allows you to pass in patch handles and convert them into\r\n% hatches that are customizable. Let's look at this contour plot which\r\n% contains several sections that I want to replace with hatches.\r\n\r\n% Create two data sets\r\nmData = membrane(1, 50);\r\npData = peaks(101)-10;\r\n\r\n% Set colormap\r\ncolormap([0 0 0; summer; 1 1 1])\r\ncontourf(mData);\r\ncaxis([min(mData(:)), max(mData(:))]);\r\n\r\nhold on\r\n[c1, h1] = contourf(pData, [2.5 2.5]-10);\r\n[c2, h2] = contourf(-pData, [2.5 2.5]+10);\r\nhold off;\r\n\r\n%%\r\n% I can make one of them a diagonal hatch and the other one a cross hatch.\r\n\r\n% Get patch objects from CONTOURGROUP\r\nhPatch1 = findobj(h1, 'Type', 'patch');\r\nhPatch2 = findobj(h2, 'Type', 'patch');\r\n\r\n% Apply Hatch Fill\r\nhh1 = hatchfill(hPatch1, 'single', -45, 3);\r\nhh2 = hatchfill(hPatch2, 'cross', 45, 3);\r\n\r\n% Remove outline\r\nset([h1, h2], 'LineStyle', 'none')\r\n\r\n% Change the cross hatch to white\r\nset(hh2, 'Color', 'white')\r\n\r\n%% |applyhatch_pluscolor| and |applyhatch_plusC|\r\n%\r\n% Both Brandon's and Brian's entries convert the distinct colors in the\r\n% whole figure into hatch patterns. The nice thing about this is that they\r\n% will also take care of legends.\r\n\r\n% Create original plot\r\nfH = gcf; colormap(jet(4));\r\nh = bar(rand(3, 4));\r\nlegend('Apple', 'Orange', 'Banana', 'Melon', 'Location', 'EastOutside');\r\n\r\n% Apply Brandon's function\r\ntH = title('Brandon''s applyhatch');\r\napplyhatch_pluscolor(fH, '\\-x.', 0, [1 0 1 0], jet(4));\r\n\r\n% Apply Brian's function\r\nset(tH, 'String', 'Brian''s applyhatch');\r\napplyhatch_plusC(fH, '\\-x.', 'rkbk');\r\n\r\nset(tH, 'String', 'Original');\r\n\r\n%%\r\n% Both of these functions were derived and inspired by some of the same\r\n% existing entries, so the functionalities are quite similar, with some\r\n% differences in syntax. For example, Brandon's function has a couple of\r\n% convenience syntaxes for inverting foreground\/background colors and\r\n% specifying different hatch line width. Brian's function can assign\r\n% arbitrary mix of colors to the hatches regardless of the original colors\r\n% used.\r\n%\r\n% I just highlighted three entries from File Exchange for creating hatches,\r\n% but there are a few other\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/?term=hatching\r\n% entries> that you may want to check out.\r\n\r\n%% Comments\r\n%\r\n% Let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=2916#respond here> or leave a comment\r\n% for <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/30733-hatchfill#comments\r\n% Neil>,\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/24021-hatch-fill-patterns-plus-color---invert#comments\r\n% Brandon>, and\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/26797-applyhatch-plusc#comments\r\n% Brian>.\r\n##### SOURCE END ##### bd0ef3038e0d425e9eb45bc17896df4d\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Jiro's picks this week are hatchfill by Neil Tandon, applyhatch_pluscolor by Brandon Levey, and applyhatch_plusC by Brian Katz.\r\n      \r\n      In graduate school, I used to spend some... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2011\/07\/15\/creating-hatched-patches\/\">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\/2916"}],"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=2916"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2916\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}