Make subplot use the full figure on buttondown click.

2 views (last 30 days)
Hello,
I have the following figure with 2 subplots for the example (I have multiple tabs with various numbers of subplots so it has to stay flexible!).
I want to be able to click on either subplot to make it take the full space (as if it was created as a single axis/plot), and if I click again on it that it reverts back to the original subplots.
So far I have only found how to make the axes invisible and thought I could somehow manipulate the figure, but I am stuck now. It is probably not the right way to go though...
hFig = figure();
ax(1) = subplot(2, 1, 1);
hold all
p(1) = plot(ax(1), [0,1], [1, 1], 'b');
ax(2) = subplot(2, 1, 2);
hold all
p(2) = plot(ax(2), [0,1], [1, 2], 'r');
set(ax(1), 'ButtonDownFcn',{@subplotZoom, ax, p})
set(ax(2), 'ButtonDownFcn',{@subplotZoom, ax, p})
function subplotZoom(src,eventdata, ax, p)
set(ax, 'Visible', 'off')
set(p, 'Visible', 'off')
end
Thanks

Accepted Answer

G A
G A on 5 Jul 2021
Edited: G A on 5 Jul 2021
The following works as you requested. You can of course modify the code in more elegant way with numbering handles as ax(n) etc . Also, I slightly cheated with the size of axes to hide boxes and tick labels of smaller axes behind.
hFig = figure();
% p0 = plot([0,1], [1, 2], 'r');
% ax0 = gca;
% pos0 = ax0.Position; % the default position is [0.13, 0.11, 0.775, 0.815]
%
% make the position slightly bigger than the default one to overlap the tick
% labels from smaller plots
pos0=[0.09, 0.1, 0.85, 0.85];
ax1 = subplot(2, 2, 1);
pos1=ax1.Position;
p1 = plot(ax1, [0,1], [1, 1], 'b');
ax2 = subplot(2, 2, 4);
pos2=ax2.Position;
p2 = plot(ax2, [0,1], [1, 2], 'r');
set(ax1, 'ButtonDownFcn',{@subplotZoom, ax1, pos1, pos0})
set(ax2, 'ButtonDownFcn',{@subplotZoom, ax2, pos2, pos0})
function subplotZoom(~,~, ax, pos, pos0)
if get(ax,'Position')==pos0
ax.Position=pos;
else
ax.Position=pos0;
axes(ax) % bring axes to the focus
end
end

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!