{"id":3660,"date":"2012-06-08T09:58:40","date_gmt":"2012-06-08T14:58:40","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=3660"},"modified":"2022-05-31T08:50:53","modified_gmt":"2022-05-31T12:50:53","slug":"customize-the-spacing-of-subplots-2","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2012\/06\/08\/customize-the-spacing-of-subplots-2\/","title":{"rendered":"Customize the spacing of subplots"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction><\/introduction>\r\n   <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911\">Brett<\/a>'s Pick this week is the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27991-tight-subplot-nh--nw--gap--marg-h--marg-w-\">Tight Subplot,<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/30542\">Pekka Kumpulainen<\/a>.\r\n   <\/p>\r\n   <p>Judging from the title of the submission, Pekka wrote and shared \"Tight Subplot\" because he was dissatisfied with the large\r\n      (default) gaps between axes generated with the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2012a\/techdoc\/ref\/subplot.html\">subplot<\/a> command. Fair enough (though with a bit of effort, one can customize the position of subplots). But what I really like about\r\n      \"Tight Subplot\" is not that the default axes spacing makes better use of the figure's real estate, but that it gives me complete--and\r\n      easy--control over horizontal and vertical gap spacing, and separately, of left-and-right and top-and-bottom margin spacing.\r\n      The syntax is easy to use:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% ha = tight_subplot(Nh, Nw, gap, marg_h, marg_w)<\/span><\/pre><p>allows me to specify the number of horizontal axes, the number of vertical axes, the gap as a scalar (or as a vector of horizontal\r\n      and vertical gap values, [gap_h gap_w]), and the height and width margins. And, of course, it conveniently returns a vector\r\n      of handles to the axes.\r\n   <\/p>\r\n   <p>By default, the axes spacing is tighter than <tt>subplot<\/tt>'s:\r\n   <\/p>\r\n   <p><tt>tight_subplot(2,2);<\/tt><\/p>\r\n   <p>yields<\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/TS2x2default.png\"> <\/p>\r\n   <p>Compare that to:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">figure\r\n<span style=\"color: #0000FF\">for<\/span> ii = 1:4\r\n    subplot(2,2,ii);\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/Subplot2x2default.png\"> <\/p>\r\n   <p>and you can clearly see how the function got its name.<\/p>\r\n   <p>Better still, ask tight_subplot for custom spacing:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">figure;\r\nha = tight_subplot(2,2,[0.05 0.025],0.05,[0.3 0.05]);<\/pre><p>creates a 2x2 array of axes with a normalized horizontal gap of 0.05; a vertical gap of 0.025; an equal height margin (top\r\n      and bottom) of 0.05; and unequal width margins--0.3 on the left, and 0.05 on the right.\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/TS2x2custom.png\"> <\/p>\r\n   <p>Pekka's function affords great control beyond just making a tight array of subplots. Thanks for sharing it, Pekka! And thanks\r\n      (again!) to <a title=\"https:\/\/sites.google.com\/site\/engelgeography\/\">Frank Engel<\/a> for the nomination. Swag on the way to both of you!\r\n   <\/p>\r\n   <p>Please continue to <a href=\"https:\/\/blogs.mathworks.com\/pick\/2012\/04\/13\/what-is-your-favorite-unrecognized-file-exchange-submission\/\">steer us to your favorite File Exchange submissions<\/a> ...we'll continue to consider your recommendations for Pick-of-the-Week recognition!\r\n   <\/p>\r\n   <p>As always, <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=3660#respond\">comments to this blog post<\/a> are welcome. Or leave a comment for Pekka <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27991-tight-subplot-nh--nw--gap--marg-h--marg-w-#comments\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_1fc24d99707b41c38c845fd1e693ed97() {\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='1fc24d99707b41c38c845fd1e693ed97 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 1fc24d99707b41c38c845fd1e693ed97';\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 = 'Brett Shoelson';\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_1fc24d99707b41c38c845fd1e693ed97()\"><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\n1fc24d99707b41c38c845fd1e693ed97 ##### SOURCE BEGIN #####\r\n%% Customize the spacing of subplots\r\n%% \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911 Brett>'s Pick this week is the\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27991-tight-subplot-nh--nw--gap--marg-h--marg-w- Tight Subplot,> by \r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/30542 Pekka Kumpulainen>.\r\n\r\n%%\r\n% Judging from the title of the submission, Pekka wrote and shared \"Tight\r\n% Subplot\" because he was dissatisfied with the large (default) gaps\r\n% between axes generated with the <https:\/\/www.mathworks.com\/help\/releases\/R2012a\/techdoc\/ref\/subplot.html subplot>\r\n% command. Fair enough (though with a bit of effort, one can customize the position\r\n% of subplots). But what I really like about \"Tight Subplot\" is not that the default\r\n% axes spacing makes better use of the figure's real estate, but that it gives me\r\n% completeREPLACE_WITH_DASH_DASHand easyREPLACE_WITH_DASH_DASHcontrol over horizontal and vertical gap spacing, and separately, \r\n% of left-and-right and top-and-bottom margin spacing. The syntax is easy to use:\r\n\r\n% ha = tight_subplot(Nh, Nw, gap, marg_h, marg_w)\r\n\r\n%%\r\n% allows me to specify the number of horizontal axes, the number of\r\n% vertical axes, the gap as a scalar (or as a vector of horizontal and vertical \r\n% gap values, [gap_h gap_w]), and the height and width margins. And, of course, it \r\n% conveniently returns a vector of handles to the axes.\r\n\r\n%% \r\n% By default, the axes spacing is tighter than |subplot|'s:\r\n%%\r\n%%\r\n% |tight_subplot(2,2);|\r\n\r\n%%\r\n% yields\r\n\r\n%%\r\n% \r\n% <<https:\/\/blogs.mathworks.com\/pick\/files\/TS2x2default.png>>\r\n% \r\n\r\n%%\r\n% Compare that to:\r\n\r\n%%\r\n%   figure\r\n%   for ii = 1:4\r\n%       subplot(2,2,ii);\r\n%   end\r\n\r\n%%\r\n% \r\n% <<https:\/\/blogs.mathworks.com\/pick\/files\/Subplot2x2default.png>>\r\n% \r\n%%\r\n% and you can clearly see how the function got its name.\r\n\r\n%%\r\n% Better still, ask tight_subplot for custom spacing:\r\n%%\r\n% \r\n%   figure;\r\n%   ha = tight_subplot(2,2,[0.05 0.025],0.05,[0.3 0.05]);\r\n% \r\n%%\r\n% creates a 2x2 array of axes with a normalized horizontal gap of 0.05;\r\n% a vertical gap of 0.025; an equal height margin (top and bottom) of 0.05;\r\n% and unequal width marginsREPLACE_WITH_DASH_DASH0.3 on the left, and 0.05 on the right.\r\n%%\r\n% \r\n% <<https:\/\/blogs.mathworks.com\/pick\/files\/TS2x2custom.png>>\r\n\r\n%% \r\n% Pekka's function affords great control beyond just making a tight array\r\n% of subplots. Thanks for sharing it, Pekka! And thanks (again!) to\r\n% <https:\/\/sites.google.com\/site\/engelgeography\/ Frank Engel> for the\r\n% nomination. Swag on the way to both of you!\r\n\r\n%%\r\n% Please continue to <https:\/\/blogs.mathworks.com\/pick\/2012\/04\/13\/what-is-your-favorite-unrecognized-file-exchange-submission\/ steer us to your favorite File Exchange submissions>\r\n% ...we'll continue to consider your recommendations for Pick-of-the-Week recognition!\r\n\r\n%% \r\n% As always, <https:\/\/blogs.mathworks.com\/pick\/?p=3660#respond comments to this blog post> are welcome. Or leave a\r\n% comment for Pekka\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/27991-tight-subplot-nh--nw--gap--marg-h--marg-w-#comments here>.\r\n\r\n##### SOURCE END ##### 1fc24d99707b41c38c845fd1e693ed97\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n   Brett's Pick this week is the Tight Subplot, by Pekka Kumpulainen.\r\n   \r\n   Judging from the title of the submission, Pekka wrote and shared \"Tight Subplot\" because he was dissatisfied with... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2012\/06\/08\/customize-the-spacing-of-subplots-2\/\">read more >><\/a><\/p>","protected":false},"author":34,"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\/3660"}],"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\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=3660"}],"version-history":[{"count":9,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/3660\/revisions"}],"predecessor-version":[{"id":15989,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/3660\/revisions\/15989"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=3660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=3660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=3660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}