Expand (maximise) subplot figure temporarily — then collapse it back

独自空忆成欢 提交于 2019-12-04 09:10:43
Evgeni Sergeev

Combining parts of these three posts, here's what I have so far:

h = subplot(2,2,1);
line(1:10, rand(1,10));
set(h, 'buttondownfcn', ['h=gca; hc = copyobj(h, gcf);' ...
    'set(hc, ''Units'', ''normal'',' ...
    ' ''Position'', [0.05 0.1 0.8 0.85],' ...
    ' ''buttondownfcn'', ''delete(gca)'');']);

It's not perfect, but it works.

Click on the axes:

Click on the expanded axes, and it disappears:

Note that this still allows you to pan, zoom, and "rotate 3D" the resulting axes. Selecting the arrow tool actually enters "Edit Mode", so it's better to unselect the tool you are using instead. For example: if you were zooming in, click on the zoom-in icon again to deselect the tool. Clicking will then "collapse" the blow-up of the axes.

The limitation so far is that you can sometimes see parts of the underlying little subplot axes underneath. If someone can recommend an elegant way to hide them, that would be a nice improvement.


EDIT Learning from this answer (using a uipanel to prevent other contents from showing through), I now have turned the solution into this:

gcaExpand.m:

function gcaExpand

    set(copyobj(gca, uipanel('Position', [0 0 1 1])), ...
       'Units', 'normal', 'OuterPosition', [0 0 1 1], ...
       'ButtonDownFcn', 'delete(get(gca, ''Parent''))'); 

end

gcaExpandable.m:

function gcaExpandable

    set(gca, 'ButtonDownFcn', [...
        'set(copyobj(gca, uipanel(''Position'', [0 0 1 1])), ' ...
        '    ''Units'', ''normal'', ''OuterPosition'', [0 0 1 1], ' ...
        '    ''ButtonDownFcn'', ''delete(get(gca, ''''Parent''''))''); ']);

end

The first one expands the current plot immediately. The second one adds the functionality where clicking onto the plot expands it. In both cases clicking again returns things back to normal.

I've placed them into the directory with all my other custom Matlab functions that I'm using on a day-to-day basis. The above can also be included in functions to be sent out.

Initially, I was going to write a custom version of subplot that applied gcaExpandable automatically, but that didn't work, because commands like plot erase the ButtonDownFcn property (as well as all other properties, except the position). According to this answer, we can avoid resetting those properties by changing the NextPlot to 'replacechildren', but that has side-effects. For example, plot no longer automatically rescales the axes. Therefore, the cleanest solution so far seems to be as above.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!