Subplot tends to leave a lot of empty space around the axes. Tight_subplot allows to adjust the spacing between the axes as well as the margins around the axes.
[ha, pos] = tight_subplot(3,2,[.01 .03],[.1 .01],[.01 .01])
for ii = 1:6; axes(ha(ii)); plot(randn(10,ii)); end
This really made my dissertation work come together nicely. I've used it hundreds of times, and appreciate it hugely! One question now that I'm using the function to generate some different plots: is it possible to specify that the bottom of two subplots take up less of the figure area than the top? I.e. the top plot would have, say 500 pixels in height, and the bottom 200? Cheers.
Nice but I had to add a shift parameter to stop my tick labels on the left from being truncated (as per below where the "bias" parameter is a new 2 element vector input arg and just added to the position as follows
'Position',[px+bias(2) py+bias(1) axw axh], ...
I guess it would be nice to have it totally auto centered (not that I know how off the top of my head) but I'm just really happy that you wrote this. Thanks.
This function does not follow subplot's default syntax because this is not subplot but another function. The third input argument is the gap between axes. Not the number-in-order of the axis to set as 'CurrentAxes' of figure as in subplot. See help for description of the input arguments.
There seemed to be an ancient version in Fex, I updated a mre recent version, which makes it easer top live with colorbar and other functions, which modify the positions of existing axes.
Yet I have one problem. When I plot odd number of subplot, say 7 plot arrayed as 4*2, there is always one blank plot that I cannot remove even though I delete the last value in ha.
Any one have any idea to fix it?
* Problem fixed by setting axis, XColor and YColorthe of the last plot invisible.
this file really helped me! however i have one question. i want to set fixed limits of the x,y axis in each subplot.For example: (axis([0 700 -500 10000])).i tried to do it but with no result. any advice of how can i do this?
I would expect to see xticks at .1, 1 and 10, but on my system they simply disappear. If I tried and add them manually the log scale gets screwed up, so there's gotta be another way around. I'm using Matlab R2012a.
Great function. Pity I came across it after I spent much of my time writing a similar function. As some people have said above, I wanted the function to work with multiple rows/columns spanning subplots. So I included that capability in the following function:
I second what Dan said. Spanning multiple columns/rows would be really useful. For example:
ha = tight_subplot(2,2,0.1,0.1,0.1)
Oops -- didn't know that rating the submission meant submitting. Excellent function that saved me tons of time in having to generate graphics through GIS. No clue why the subplot command has this issue, but thanks for addressing it!
Still, I have one suggestion: Instead of saying "axes(ha(ii));" in the example a better way may be to say "subplot(ha(ii));". Using axes gave me unpredictable behaviour when setting up two plots side by side. I would (almost always) plot to the axes on the right, no mater how often I tried giving the "axes(ha(1))" command (or "axes(ha(2))" for that matter). Using subplot solved that. Don't know why though ...
Good work. I needed to create several plots with several sub-plots within a loop. Had trouble with empty plots as the result and Matlab was broke until I restarted it. Even a plot(1:100, rand(1, 100)) would return an empty figure. Anyway, I returned the value for the axes position matrix "ha", and removed the internal axes command. When I was ready to plot in one of the figures I had being created to a specific subplot location I called:
Thank you for this contribution. I like it very much.
In my opinion the input check should be like that:
if nargin<3 || isempty(gap); gap = .02; end
if nargin<4 || isempty(marg_h); marg_h = .05; end
if nargin<5 || isempty(marg_w); marg_w = .05; end
So you can modify marg_w and take the defaults for the other parameters.