0

我经常遇到 MATLAB 图中的图例问题,并想想出一种方法来避免它们在未来出现。

我想做的是以下几点:

  1. 创建一个固定大小的图形:

    f = figure('Position',[0 0 800 600])

  2. 绘制我想在此图中绘制的任何内容

    x = -pi:0.01:pi
    plot(x,sin(x),x,cos(x),x,tan(x))

  3. 在图的底部添加一个图例,而不调整图的大小(可以说,我可以使图“更高”,但我希望图例能够超越图、轴和其他所有内容). 如果可能的话,我还想使用legendflex包来创建图例(不确定这是否会引起任何问题)。

有谁知道我该怎么做?

4

1 回答 1

2

我使用的是 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));

阿尔诺

于 2013-05-02T09:02:07.403 回答