{"id":8602,"date":"2017-05-19T09:00:07","date_gmt":"2017-05-19T13:00:07","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=8602"},"modified":"2017-05-19T01:13:50","modified_gmt":"2017-05-19T05:13:50","slug":"subplot-position-calculator","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2017\/05\/19\/subplot-position-calculator\/","title":{"rendered":"Subplot Position Calculator"},"content":{"rendered":"\r\n\r\n<div class=\"content\"><p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871\">Jiro<\/a>&#8216;s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/42763-subplot-position-calculator\">Subplot Position Calculator<\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2544496\">Christopher Hummersone<\/a>.<\/p><p><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/subplot.html\"><tt>subplot<\/tt><\/a> is a nice way to place a grid of axes on a single figure. But the spacing is sometimes a bit too generous, leaving too little room for the graph.<\/p><pre class=\"codeinput\">nRows = 3;\r\nnCols = 2;\r\n<span class=\"keyword\">for<\/span> m = 1:nRows\r\n    <span class=\"keyword\">for<\/span> n = 1:nCols\r\n        subplot(nRows,nCols,(m-1)*nCols+n)\r\n        plot(rand(20,1))\r\n    <span class=\"keyword\">end<\/span>\r\n<span class=\"keyword\">end<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_subplot_position\/potw_subplot_position_01.png\" alt=\"\"> <p>In that case, you can use <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/axes.html\"><tt>axes<\/tt><\/a> or <tt>subplot<\/tt> to specify the actual placement of the axes.<\/p><pre class=\"codeinput\">figure\r\nsubplot(<span class=\"string\">'position'<\/span>,[0.05 0.1 0.45 0.8])\r\nplot(rand(20,1))\r\nsubplot(<span class=\"string\">'position'<\/span>,[0.55 0.1 0.4 0.8])\r\nplot(rand(20,1))\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_subplot_position\/potw_subplot_position_02.png\" alt=\"\"> <p>But this defeats the purpose of <tt>subplot<\/tt> because you have to calculate and specify the position manually. This is where Christopher&#8217;s entry comes in handy. It lets you specify the grid layout, with optional parameters, and it returns a set of position vectors which you can feed into <tt>subplot<\/tt> or <tt>axes<\/tt>.<\/p><p>The default is a tight layout with no margin.<\/p><pre class=\"codeinput\">pos = iosr.figures.subfigrid(nRows,nCols);\r\nfigure\r\n<span class=\"keyword\">for<\/span> m = 1:nRows\r\n    <span class=\"keyword\">for<\/span> n = 1:nCols\r\n        subplot(<span class=\"string\">'Position'<\/span>,pos(m,:,n))\r\n        plot(rand(20,1))\r\n    <span class=\"keyword\">end<\/span>\r\n<span class=\"keyword\">end<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_subplot_position\/potw_subplot_position_03.png\" alt=\"\"> <p>You can specify the spacing and the scaling by passing in a couple of optional arguments.<\/p><pre class=\"codeinput\">pos = iosr.figures.subfigrid(nRows,nCols,[0.1 0.05 0.05 0.1],[.95 .95]);\r\nfigure\r\n<span class=\"keyword\">for<\/span> m = 1:nRows\r\n    <span class=\"keyword\">for<\/span> n = 1:nCols\r\n        subplot(<span class=\"string\">'Position'<\/span>,pos(m,:,n))\r\n        plot(rand(20,1))\r\n    <span class=\"keyword\">end<\/span>\r\n<span class=\"keyword\">end<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_subplot_position\/potw_subplot_position_04.png\" alt=\"\"> <p>As some of you may have guessed, his function is part of a <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_oop\/scoping-classes-with-packages.html\">package<\/a> (IoSR Matlab Toolbox), and the other functionalities are also highlighted in his other <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2544496?detail=fileexchange\">File Exchange entries<\/a>.<\/p><p>In addition to the usefulness of this function, I like Christopher&#8217;s entry because it is well-written with plenty of error-checking and has good help with examples. I haven&#8217;t had a chance to check out his other functions in his toolbox, but I would expect the same kind of quality as seen in this one.<\/p><p><b>Comments<\/b><\/p><p>Give it a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=8602#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/42763-subplot-position-calculator#comment\">comment<\/a> for Christopher.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_4f167252de084085b179a32a7f341e01() {\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='4f167252de084085b179a32a7f341e01 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 4f167252de084085b179a32a7f341e01';\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        copyright = 'Copyright 2017 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 copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\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     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_4f167252de084085b179a32a7f341e01()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2017a<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; R2017a<br><\/p><\/div><!--\r\n4f167252de084085b179a32a7f341e01 ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/869871 Jiro>'s\r\n% pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/42763-subplot-position-calculator Subplot\r\n% Position Calculator> by\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2544496\r\n% Christopher Hummersone>.\r\n%\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/subplot.html |subplot|> is a\r\n% nice way to place a grid of axes on a single figure. But the spacing is\r\n% sometimes a bit too generous, leaving too little room for the graph.\r\n\r\nnRows = 3;\r\nnCols = 2;\r\nfor m = 1:nRows\r\n    for n = 1:nCols\r\n        subplot(nRows,nCols,(m-1)*nCols+n)\r\n        plot(rand(20,1))\r\n    end\r\nend\r\n\r\n%%\r\n% In that case, you can use\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/axes.html |axes|> or |subplot|\r\n% to specify the actual placement of the axes.\r\n\r\nfigure\r\nsubplot('position',[0.05 0.1 0.45 0.8])\r\nplot(rand(20,1))\r\nsubplot('position',[0.55 0.1 0.4 0.8])\r\nplot(rand(20,1))\r\n\r\n%%\r\n% But this defeats the purpose of |subplot| because you have to calculate\r\n% and specify the position manually. This is where Christopher's entry\r\n% comes in handy. It lets you specify the grid layout, with optional\r\n% parameters, and it returns a set of position vectors which you can feed\r\n% into |subplot| or |axes|.\r\n%\r\n% The default is a tight layout with no margin.\r\n\r\npos = iosr.figures.subfigrid(nRows,nCols);\r\nfigure\r\nfor m = 1:nRows\r\n    for n = 1:nCols\r\n        subplot('Position',pos(m,:,n))\r\n        plot(rand(20,1))\r\n    end\r\nend\r\n\r\n%%\r\n% You can specify the spacing and the scaling by passing in a couple of\r\n% optional arguments.\r\n\r\npos = iosr.figures.subfigrid(nRows,nCols,[0.1 0.05 0.05 0.1],[.95 .95]);\r\nfigure\r\nfor m = 1:nRows\r\n    for n = 1:nCols\r\n        subplot('Position',pos(m,:,n))\r\n        plot(rand(20,1))\r\n    end\r\nend\r\n\r\n%%\r\n% As some of you may have guessed, his function is part of a\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/matlab_oop\/scoping-classes-with-packages.html\r\n% package> (IoSR Matlab Toolbox), and the other functionalities are also\r\n% highlighted in his other\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2544496?detail=fileexchange\r\n% File Exchange entries>.\r\n%\r\n% In addition to the usefulness of this function, I like Christopher's\r\n% entry because it is well-written with plenty of error-checking and has\r\n% good help with examples. I haven't had a chance to check out his other\r\n% functions in his toolbox, but I would expect the same kind of quality\r\n% as seen in this one.\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=8602#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/42763-subplot-position-calculator#comment\r\n% comment> for Christopher.\r\n\r\n##### SOURCE END ##### 4f167252de084085b179a32a7f341e01\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_subplot_position\/potw_subplot_position_01.png\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n\r\nJiro&#8216;s pick this week is Subplot Position Calculator by Christopher Hummersone.subplot is a nice way to place a grid of axes on a single figure. But the spacing is sometimes a bit too&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2017\/05\/19\/subplot-position-calculator\/\">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\/8602"}],"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=8602"}],"version-history":[{"count":5,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8602\/revisions"}],"predecessor-version":[{"id":8607,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8602\/revisions\/8607"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=8602"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=8602"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=8602"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}