Question

I have the following function that is supposed to create subplots that are tightly spaced within a row, and distributed with spacing in each column. It also lets you set buffer sizes for the edges:

function fill_graph()
for x=1:3
    for y=1:3
        ax = subplot(3,3,(x-1)*3+y);
        left_buffer = .05;
        right_buffer = .025;
        x_pos = left_buffer+(x-1)*(1/3*(1-left_buffer-right_buffer));
        width = 1/3*(1-left_buffer-right_buffer);
        bottom_buffer = .1;
        top_buffer = .05;
        spacing=.07;
        height = (1/3)*(1-2*spacing-top_buffer-bottom_buffer);
        y_pos = bottom_buffer+(y-1)*(spacing+height);
        set(ax,'position',[x_pos,y_pos,width,height])
        if x>1
            set(gca,'yTickLabel','');
            ylabel('');
        end
        if y>1 || x~=2
            xlabel('')
        end
    end
end

When I run the function, the bottom row of plots and the leftmost plot on the middle row all disappear.

I'm aware this probably has something to do with the fact that subplots will disappear when they overlap with one another, but regardless of how far apart I set the spacing, I can't get the bottom row to appear (the only way I've found is setting the bottom buffer above .34, which only works some of the time). I've tried switching 'position' to 'outerposition', which should make it so that none of them overlap, but that doesn't seem to work either.

I have seen Second subplot disappearing, but when I specify the position in that manner (subplot('position',x_pos,y_pos,width,height)), it writes over the plots that are already there. If I place that code before I plot, the plots resize as soon as I issue the plot command.

Was it helpful?

Solution

Try replacing subplot(....) with axes().

The command subplot checks all existing axes on the figure to see if any come near the required space, or were likely generated by a previous, identical subplot command. Sounds like you are spending all of your time/effort reverse engineering this part of Matlab's algorithm.

The command axes just makes a new axis on the figure, no matter what else is already there. For example, I have built plots where the axes intentionally overlap. As long as you are doing all the positioning work yourself, don't waste your time with subplot.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top