我使用的是 Octave,而不是 MATLAB,但以下是否有效(或至少让您更接近您想要的)?
% Create the figure and plot
f = figure('Position',[0 0 800 600]);
x = -pi:0.01:pi;
plot(x,sin(x),x,cos(x),x,tan(x));
% Set axes and figure units to pixels, get current positions
set(f,'Units','pixels')
set(gca,'Units','pixels')
fig_pos = get(f,'position');
old_ax_pos = get(gca,'position');
% Add a legend et get its position too
h = legend('L1','L2','L3','location','southoutside');
set(h,'Units','pixels')
leg_pos = get(h,'position');
% Get the new axes position, look at how much it shifted
new_ax_pos = get(gca,'position');
pixel_shift = new_ax_pos - old_ax_pos; % y position shift is positive (axes moved up), y height shift is negative (axes got smaller)
% Make figure taller and restore axes height to their initial value
set(f,'position',fig_pos - [0 0 0 pixel_shift(4)]);
set(h,'position',leg_pos)
set(gca,'position',old_ax_pos + [0 pixel_shift(2) 0 0])
% Create a new figure without legend for comparing
f2 = figure('Position',[0 0 800 600]);
x = -pi:0.01:pi;
plot(x,sin(x),x,cos(x),x,tan(x));
阿尔诺