subplot 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.
nRows = 3; nCols = 2; for m = 1:nRows for n = 1:nCols subplot(nRows,nCols,(m-1)*nCols+n) plot(rand(20,1)) end end
In that case, you can use axes or subplot to specify the actual placement of the axes.
figure subplot('position',[0.05 0.1 0.45 0.8]) plot(rand(20,1)) subplot('position',[0.55 0.1 0.4 0.8]) plot(rand(20,1))
But this defeats the purpose of subplot because you have to calculate and specify the position manually. This is where Christopher’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 subplot or axes.
The default is a tight layout with no margin.
pos = iosr.figures.subfigrid(nRows,nCols); figure for m = 1:nRows for n = 1:nCols subplot('Position',pos(m,:,n)) plot(rand(20,1)) end end
You can specify the spacing and the scaling by passing in a couple of optional arguments.
pos = iosr.figures.subfigrid(nRows,nCols,[0.1 0.05 0.05 0.1],[.95 .95]); figure for m = 1:nRows for n = 1:nCols subplot('Position',pos(m,:,n)) plot(rand(20,1)) end end
In addition to the usefulness of this function, I like Christopher’s entry because it is well-written with plenty of error-checking and has good help with examples. I haven’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.
Get the MATLAB code
Published with MATLAB® R2017a
Comments are closed.
1 CommentsOldest to Newest
Yet another PotW dealing with subplot. I think the full functionality of subfigrid is actually contained in subplot, if TMW would just exposed the hard coded “inset” parameter through either the figure properties or the appdata of the figure. I mentioned that I have an enhancement request the last time you picked a subplot FEX submission (http://blogs.mathworks.com/pick/2012/12/21/figure-margins-subplot-spacings-and-more/?s_tid=srchtitle#comment-17796). Maybe someday TMW will improve subplot.